小议CslaIdentity的安全性

来源:互联网 发布:如何下载mysql数据库 编辑:程序博客网 时间:2024/04/29 20:17
    /// <summary>    /// 客户端登录类    /// </summary>    public class OEAPrincipal : CslaPrincipal    {        private OEAPrincipal(IIdentity identity)            : base(identity)        { }        public static bool Login(string username, string password)        {            var identity = OEAIdentity.GetIdentity(username, password);            if (identity.IsAuthenticated)            {                OEAPrincipal principal = new OEAPrincipal(identity);                Csla.ApplicationContext.User = principal;                return true;            }            else            {                Csla.ApplicationContext.User = new UnauthenticatedPrincipal();                return false;            }        }        public static void Logout()        {            Csla.ApplicationContext.User = new UnauthenticatedPrincipal();        }    }



    public sealed class OEAIdentity : CslaIdentity    {        #region  Factory Methods        internal static OEAIdentity GetIdentity(string username, string password)        {            //如果注入以下代码,哪么问题就严重了            //var hackuser = DataPortal.Fetch<OEAIdentity>(new UsernameCriteria(username, password));            //hackuser.Roles.Add("SuperMan");            //return hackuser;            return DataPortal.Fetch<OEAIdentity>(new UsernameCriteria(username, password));        }}

因为以上的两个类都必需运行在客户端,所以存在代码被非法修改的可能。如果客户端在OEAIdentity中注入了代码,哪么问题就严重了。

目前好的解决方案是在服务端建立登录用户的列表,并分配唯一的Guid值,每次检查权限时,都在服务端的用户列表中进行检索。