2016-05-25 21 views
0

In meiner ASP.NET MVC 5-Anwendung muss ich benutzerdefinierte Authentifizierung verwenden. Grundsätzlich eine benutzerdefinierte Bibliothek, auf der ich eine Methode aufrufen und die ein Objekt zurückgibt, das Informationen über den Benutzer enthält.ASP.NET MVC 5: Benutzerdefinierte Authentifizierung

Ich habe eine neue MVC 5-Anwendung erstellt und die Option "Keine Authentifizierung" ausgewählt. Dann habe ich ein HTTP-Modul hinzugefügt, die derzeit wie folgt aussieht:

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); 
} 

Meine claimstransformer wie folgt aussieht:

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")); 
} 

Die wichtigste Frage, die ich gegenüber ist, dass der Schritt der Authentifizierung verlangt wird jede Anfrage, obwohl eine Sitzung existiert. Wie kann ich feststellen, dass eine Sitzung vorhanden ist, und einfach die Sitzung laden, anstatt den gesamten Authentifizierungsprozess zu durchlaufen?

Ein weiteres Problem ist, dass der Aufruf der Authentifizierungsbibliothek eine Weile dauern kann. Ich denke, idealerweise sollte es auch zum Schadenstransformator verlegt werden?

Alle Ideen, um diesen Code weiter zu verbessern, werden ebenfalls sehr geschätzt.

Bitte lassen Sie mich wissen, wenn etwas nicht klar ist oder wenn ich detailliertere Informationen bereitstellen muss.

+1

Sieht so aus, als müssten Sie die Ansprüche zwischenspeichern - das ähnelt Ihrem Ziel. http://stackoverflow.com/questions/16904639/simple-claims-transformation-and-caching-w-windows-authentication –

Antwort

0

Es scheint mir, dass Sie keine Authentifizierungsinformationen mit jeder Anfrage nach der Authentifizierung bereitstellen. Können Sie überprüfen, ob nach der Authentifizierung ein Sitzungscookie oder ein Authentifizierungsheader mit jeder Anfrage gesendet wurde?

Verwandte Themen