首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

OpenExpressApp 平台:使用 CSLA 類庫實作使用者管理與登入驗證

     編輯:關於.NET

資訊系統開發平台 OpenExpressApp (OEA) 採用 RBAC 權限模型,基礎在於使用者管理。CSLA 框架提供 BusinessPrincipalBase、CslaIdentity 等基礎類別,快速實作自訂 Principal/Identity。

自訂 Principal:OEAPrincipal

繼承 BusinessPrincipalBase,實作靜態 Login/Logout:

public class OEAPrincipal : BusinessPrincipalBase {
    private OEAPrincipal(IIdentity identity) : base(identity) { }
    public static bool Login(string username, string password) {
        var identity = OEAIdentity.GetIdentity(username, password);
        if (identity.IsAuthenticated) {
            Csla.ApplicationContext.User = new OEAPrincipal(identity);
            return true;
        }
        Csla.ApplicationContext.User = new UnauthenticatedPrincipal();
        return false;
    }
    public static void Logout() => Csla.ApplicationContext.User = new UnauthenticatedPrincipal();
}

自訂 Identity:OEAIdentity

繼承 CslaIdentity,透過 DataPortal.Fetch 向伺服器驗證並載入角色:

[Serializable]
public class OEAIdentity : CslaIdentity {
    public User User { get; set; }
    public new OrgPositions Roles { get; private set; }
    private OEAIdentity() { }
    internal static OEAIdentity GetIdentity(string username, string password) =>
        DataPortal.Fetch<OEAIdentity>(new UsernameCriteria(username, password));
    private void DataPortal_Fetch(UsernameCriteria criteria) {
        User = UserList.Get(criteria.Username, criteria.Password);
        if (User != null) {
            base.Name = User.Name; base.IsAuthenticated = true;
            Roles = OrgPositions.GetList(User.Id); // 從安全存儲載入角色
        } else { base.Name = string.Empty; base.IsAuthenticated = false; Roles = null; }
    }
}

登入成功後,Csla.ApplicationContext.User 保存 Principal,後續授權檢查可透過 (Csla.ApplicationContext.User.Identity as OEAIdentity).Roles 取得角色集合。下一篇將介紹功能權限控制。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved