2016-10-25 5 views
0

Für meine WebAPI, ich verwende: public void ConfigureAuth (IAppBuilder app) {app.UseIdentityServerBearerTokenAuthentication und Userinfo

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
{ 
    Authority = ConfigurationManager.AppSettings[ "ida:Authority" ], 
    RequiredScopes = new[ ] 
    { 
     "XXXXAPI" 
    } 
}); 

}

ich authentifizieren in Ordnung, aber ich brauche meine Rollen zu bekommen und andere Informationen, die vom UserInfo-Endpunkt bereitgestellt werden.

Führt UseIdentityServerBearerTokenAuthentication dies automatisch durch oder gibt es ein Ereignis wie OpenIdConnectAuthenticationNotifications.AuthroizationCodeReceived, das ich verwenden sollte, um die ClaimsIdentity festzulegen?

+0

Wenn Sie sich "authentifizieren", haben Sie ein ClaimsPrincipal mit den vom Zugriffstoken bereitgestellten Ansprüchen. Enthalten Sie entweder, was Sie in den Zugriffstoken benötigen, oder führen Sie einen separaten Anruf aus, um die benötigten Informationen abzurufen. Ich bevorzuge den ersten Ansatz (weniger gesprächig). –

Antwort

0

app.UseIdentityServerBearerTokenAuthentication wird festgelegt die Rollen und Bereiche automatisch.

Aus einem unbekannten Grund habe ich nur Daten aus dem GetProfileDataAsync des UserService nur zurückgegeben, wenn der Aufrufer UserInfoEndpoint war. Sobald ich diesen Teil des Codes los war, wurden alle Rollen automatisch ausgefüllt.

0

Sie müssen OAuth2 + JWT und einige benutzerdefinierte Konfigurationen verwenden, um Benutzerrollen und andere Ansprüche im Zugriffstoken zu speichern.

  1. diese Werte in Web.config hinzufügen

    <appSettings> 
        <add key="as:AudienceId" value="414e1927a3884f68abc79f7283837fd1" /> 
        <add key="as:AudienceSecret" value="qMCdFDQuF23RV1Y-1Gq9L3cF3VmuFwVbam4fMTdAfpo" /> 
    </appSettings> 
    
  2. eine CustomJwtFormat Klasse schreiben

    using Microsoft.Owin.Security; 
    using Microsoft.Owin.Security.DataHandler.Encoder; 
    using System; 
    using System.Configuration; 
    using System.IdentityModel.Tokens; 
    using Thinktecture.IdentityModel.Tokens; 
    public class CustomJwtFormat : ISecureDataFormat<AuthenticationTicket> 
    { 
    
    private readonly string _issuer = string.Empty; 
    
    public CustomJwtFormat(string issuer) 
    { 
        _issuer = issuer; 
    } 
    
    public string Protect(AuthenticationTicket data) 
    { 
        if (data == null) 
        { 
         throw new ArgumentNullException("data"); 
        } 
    
        string audienceId = ConfigurationManager.AppSettings["as:AudienceId"]; 
    
        string symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"]; 
    
        var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64); 
    
        var signingKey = new HmacSigningCredentials(keyByteArray); 
    
        var issued = data.Properties.IssuedUtc; 
    
        var expires = data.Properties.ExpiresUtc; 
    
        var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey); 
    
        var handler = new JwtSecurityTokenHandler(); 
    
        var jwt = handler.WriteToken(token); 
    
        return jwt; 
    } 
    } 
    
  3. eine authenticationProvider individuelle Erstellen

    using System.Collections.Generic; 
    using System.Linq; 
    using System.Security.Claims; 
    using System.Threading.Tasks; 
    using Microsoft.Owin.Security; 
    using Microsoft.Owin.Security.OAuth; 
    public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider 
    { 
    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
    { 
        context.Validated(); 
        return Task.FromResult<object>(null); 
    } 
    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 
    
        using (AuthRepository _repo = new AuthRepository()) 
        { 
         User user = await _repo.FindUser(context.UserName, context.Password); 
    
         if (user == null) 
         { 
          context.SetError("invalid_grant", "The user name or password is incorrect."); 
          return; 
         } 
        } 
    
        var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
        identity.AddClaim(new Claim("unique_name", context.UserName)); 
        identity.AddClaim(new Claim("role", "user")); 
    
        context.Validated(identity); 
    } 
    } 
    
  4. Konfigurieren der benutzerdefinierten

    private static void ConfigureOAuthTokenGeneration(IAppBuilder app) 
    { 
    
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() 
        { 
         //For Dev enviroment only (on production should be AllowInsecureHttp = false) 
         AllowInsecureHttp = true, 
         TokenEndpointPath = new PathString("/oauth/token"), 
         AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
         Provider = new ApplicationOAuthProvider(), 
         AccessTokenFormat = new CustomJwtFormat("http://localhost/") 
        }; 
    
        // OAuth 2.0 Bearer Access Token Generation 
        app.UseOAuthAuthorizationServer(OAuthServerOptions); 
    } 
    private static void ConfigureOAuthTokenConsumption(IAppBuilder app) 
    { 
    
        var issuer = "http://localhost/"; 
        string audienceId = ConfigurationManager.AppSettings["as:AudienceId"]; 
        byte[] audienceSecret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["as:AudienceSecret"]); 
    
        // Api controllers with an [Authorize] attribute will be validated with JWT 
        app.UseJwtBearerAuthentication(
         new JwtBearerAuthenticationOptions 
         { 
          AuthenticationMode = AuthenticationMode.Active, 
          AllowedAudiences = new[] { audienceId }, 
          IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[] 
          { 
           new SymmetricKeyIssuerSecurityTokenProvider(issuer, audienceSecret) 
          } 
         }); 
    } 
    public void Configuration(IAppBuilder app(
    { 
          app.UseAutofacMvc(); 
        ConfigureOAuthTokenGeneration(app); 
        ConfigureOAuthTokenConsumption(app); 
        //other codes 
    } 
    
  5. Einstellung Wenn Sie userinfo von Zugriffstoken, tun Sie es erhalten will, wie folgt:

    public static string GetUserNameFromOAuth(HttpRequestMessage Request) 
        { 
        if (Request.Headers.Contains("Authorization")) 
        { 
         var authHeader = Request.Headers.GetValues("Authorization"); 
         var authEncoded = authHeader.FirstOrDefault(); 
         var authList = authEncoded.Split(' '); 
         var payload = authList[1]; 
    
         var symmetricKeyAsBase64 = ConfigurationManager.AppSettings["as:AudienceSecret"]; 
         var keyByteArray = TextEncodings.Base64Url.Decode(symmetricKeyAsBase64); 
    
         var signingKey = new HmacSigningCredentials(keyByteArray); 
         string token = JWT.JsonWebToken.Decode(payload, keyByteArray); 
         var jsonObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(token); 
         var userName = jsonObject.FirstOrDefault(p => p.Key == "unique_name").Value; 
         return userName; 
        } 
        return ""; 
        } 
    
  6. -Test in Postbote:

enter image description here

Verwandte Themen