簡體   English   中英

ASP.NET MVC 5:自定義身份驗證

[英]ASP.NET MVC 5: Custom Authentication

在我的ASP.NET MVC 5應用程序中,我需要使用自定義身份驗證。 基本上是一個自定義庫,我在該庫上調用方法,並返回一個包含有關用戶信息的對象。

我創建了一個新的MVC 5應用程序,並選擇了“無身份驗證”選項。 然后,我添加了一個Http模塊,該模塊當前如下所示:

private void Context_AuthenticateRequest(object sender, EventArgs e)
{
    // Make the call to authenticate.
    // This returns an object with user information.
    AuthResult result = new AuthLib().SignOn();

    // Inspect the returned object and create a list claims.
    var claims = new List<Claim>
    {
        new Claim(ClaimTypes.NameIdentifier, result.Username),
        new Claim(ClaimTypes.GivenName, result.Name)
    }
    claims.AddRange(result.Groups.Select(g => new Claim(ClaimType.Role, g));

    // Create principal and attach to context
    var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, "Sso");
    HttpContext.Current.User = principal;
    Thread.CurrentPrincipal = principal;
}

private void Context_PostAuthenticateRequest(object sender, EventArgs e)
{
    var principal = ClaimsPrincipal.Current;
    ClaimsAuthenticationManager transformer = FederatedAuthentication.SessionAuthenticationModule.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager;
    transformer.Authenticate(string.Empty, principal);
}

我的ClaimTransformer看起來像這樣:

public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
{
    if (!incomingPrincipal.Identity.IsAuthenticated)
    {
        return base.Authenticate(resourceName, incomingPrincipal);
    }

    ClaimsPrincipal newPrincipal = CreateApplicationPrincipal(incomingPrincipal);

    EstablishSession(newPrincipal);

    return newPrincipal;
}

private void EstablishSession(ClaimsPrincipal newPrincipal)
{
    var sessionToken = new SessionSecurityToken(newPrincipal, TimeSpan.FromHours(8));
    FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionToken);
}

private ClaimsPrincipal CreateApplicationPrincipal(ClaimsPrincipal incomingPrincipal)
{
    // Convert AD group to known role in our application.
    string group = incomingPrincipal.FindFirst(ClaimTypes.Role).Value;
    string role = new ADGroupToRoleConverter().ConvertADGroupToRole(group);

    // Add claims for group.
    // These would be loaded from a db.
    List<Claim> claims = new ClaimDb().GetClaimsForRole(role);

    // Just copy the claims for id and given name.
    claims.Add(incomingPrincipal.FindFirst(ClaimTypes.NameIdentifier));
    claims.Add(incomingPrincipal.FindFirst(ClaimTypes.GivenName));

    return new ClaimsPrincipal(new ClaimsIdentity(claims, "MyApp"));
}

我面臨的主要問題是,即使存在會話,也會為每個請求調用身份驗證步驟。 如何檢測到會話存在,然后僅加載會話,而不執行整個身份驗證過程。

另一個問題是對身份驗證庫的調用可能需要一段時間。 我想理想情況下也應該將其移至聲明轉換器?

任何進一步改進此代碼的想法也將受到贊賞。

如果有不清楚的地方或需要提供更詳細的信息,請告訴我。

在我看來,您在身份驗證后沒有為每個請求提供身份驗證信息。 身份驗證發生后,您是否可以驗證每個請求都發送了一些會話cookie或身份驗證標頭?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM