2017-01-02 4 views
4

Frohes neues Jahr an alle ...IdentityServer4 IdentityServer3.AccessTokenValidation

konfiguriert ich eine IdentityServer4, und ich kann erfolgreich ASP.net Core-Web-API-Aufrufe. Aber für asp.net Framework 4.5.2 Web Apis, Ich bekam {"Antwort Statuscode zeigt nicht Erfolg: 401 (nicht autorisiert).") Fehler von einem .NET Framework Web API. Ich möchte deine Hilfe und Meinung fragen.

Ich suchte das Thema mit IS4 und fand einige Einträge über die Kompatibilität von IdentityServer3.AccessTokenValidation. Und nach den Antworten habe ich ein Signaturzertifikat geladen und AddSigningCredential anstelle von AddTemporarySigninCredential aufgerufen. x509certificate ist ein lokal erstelltes Zertifikat. und ich habe die IdentityServer3.AccessTokenValidation-Version auf Version 2.1.0 aktualisiert.

Noch habe ich den Fehler. Jede Hilfe wird geschätzt.

Grüße und vielen Dank für Ihre große Mühe.

IdentityServer 4 Seite: Startup.cs

public void ConfigureServices(IServiceCollection services) 
     { 
       services 
       .AddIdentityServer()     
       //.AddTemporarySigningCredential() 
       .AddSigningCredential(x509Certificate) 
       .AddInMemoryIdentityResources(Config.GetIdentityResources()) 
       .AddInMemoryApiResources(Config.GetApiResources()) 
       .AddInMemoryClients(Config.GetClients()) 
       .AddAspNetIdentity<ApplicationUser>(); 
} 

Config.cs

public static IEnumerable<ApiResource> GetApiResources() 
      { 
       return new List<ApiResource> 
       { 
        new ApiResource("AuthorizationWebApi","Authorization Web API .NET Core"), 
        new ApiResource("AuthorizationWebApiNetFramework","Authorization Web API NET Framework"), 
       new ApiResource("api1", "Empty Test Api") 
       }; 

      } 

     public static IEnumerable<Client> GetClients() 
     { 
      return new List<Client> { 
new Client { 
        ClientId = "silicon", 
        ClientName = "console app", 
        AllowedGrantTypes = GrantTypes.ClientCredentials, 
        ClientSecrets = { new Secret("abcdef".Sha256())}, 
        AllowedScopes = new List<string>{ 
        "AuthorizationWebApiNetFramework" 
        } 

       }, 
       new Client 
       { 
        ClientId = "MYUX", 
        ClientName = "MYUX MVC Client", 
        AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, 
        RequireConsent = false, 
        ClientSecrets= {new Secret("abcdef".Sha256()) }, 
        RedirectUris = { "http://localhost:5002/signin-oidc" }, 
        PostLogoutRedirectUris = {"http://localhost:5002"}, 

        AllowedScopes = { 
         IdentityServerConstants.StandardScopes.OpenId, 
         IdentityServerConstants.StandardScopes.Profile,       
         "custom.profile", 
         "AuthorizationWebApi", 
         "AuthorizationWebApiNetFramework" 
        }, 
        AllowOfflineAccess = true 
       } 
      }; 
     } 

.NET Framework-API Side

public void Configuration(IAppBuilder app) 
     { 
      //ConfigureAuth(app); 
      app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
      app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
      { 
       Authority = "http://www.abcdefgh.com:5000", 
       ValidationMode = ValidationMode.ValidationEndpoint, 
       RequiredScopes = new[] { "AuthorizationWebApiNETFramework" } 

      }); 
      //configure web api 
      var config = new HttpConfiguration(); 
      config.MapHttpAttributeRoutes(); 

      //require authentication for all controllers 

      config.Filters.Add(new AuthorizeAttribute()); 

      app.UseWebApi(config); 
     } 

rufenden Seite:

try 
      { 
       ViewData["Message"] = "Authorization Test."; 
       var accessToken = await HttpContext.Authentication.GetTokenAsync("access_token"); 
       var authorizationApiClient = new HttpClient(); 
       authorizationApiClient.SetBearerToken(accessToken); 
       var content = await authorizationApiClient.GetStringAsync("http://localhost:13243/values"); 
       return View(); 
      } 
      catch (Exception ex) 
      { 
       throw; 
      } 

oder durch eine Konsolenanwendung ...

try 
{ 
    // discover endpoints from metadata 
    var disco = await DiscoveryClient.GetAsync("http://www.abcdefgh.com:5000"); 

    var tokenClient = new TokenClient(disco.TokenEndpoint, "silicon", "abcdef"); 
    var tokenResponse = await tokenClient.RequestClientCredentialsAsync("AuthorizationWebApiNetFramework"); 

    if (tokenResponse.IsError) 
    { 
     Console.WriteLine(tokenResponse.Error); 
     return; 
    } 

    Console.WriteLine(tokenResponse.Json); 

    var client = new HttpClient(); 
    client.SetBearerToken(tokenResponse.AccessToken); 

    var response = await client.GetAsync("http://localhost:13243/values"); 
    if (!response.IsSuccessStatusCode) 
    { 
     Console.WriteLine(response.StatusCode); 
    } 
    else 
    { 
     var content = await response.Content.ReadAsStringAsync(); 
     Console.WriteLine(JArray.Parse(content)); 
    } 
} 
catch (Exception) 
{ 
    throw; 
}  

EDIT: auf 4.5.2 Api Seite: ich aus der Leitung ValidationMode = ValidationMode.ValidationEndpoint kommentiert. Ich habe diese Zeile hinzugefügt, indem ich die IS3-Dokumentation befolgte. Danke an alle.

+0

Ich würde vorschlagen, mit den Proben beginnen und bestätigen, dass sie funktionieren. Dann beginnen Sie mit den Beispielen, um die Unterschiede in Ihren benutzerdefinierten Projekten zu vergleichen. –

+0

Was sagt das IDsrv4-Protokoll zu der Zeit, zu der Sie den Fehler 401 erhalten? –

+0

Danke Jungs, @BrockAllen, wie ich sagte, ich kann ASP.Net Core MVC mit Open ID connect authentifizieren und ASP.Net Core WebApi mit Client-Anmeldeinformationen mit meinem ASP.Net Core IS4 authentifizieren. Aber ich habe ein Problem mit 4.5.2 ApiResource. Jonas Axelsson Ich sehe Token erfolgreich erstellt, aber wie ich mich erinnere, geschieht nichts, wenn ich GetAsync von WebApi aufrufen. Ich werde es heute überprüfen :). Grüße – ozgurozkanakdemirci

Antwort

3

Entfernen Sie die folgende Zeile in der WebAPI AccessStoken-Validierungs-Middleware.

ValidationMode = ValidationMode.ValidationEndpoint 

Das Ergebnis sollte wie folgt aussehen:

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
{ 
    Authority = "http://www.abcdefgh.com:5000", 
    RequiredScopes = new[] { "AuthorizationWebApiNETFramework" } 
});