2017-01-06 2 views
3

私は自分のプロジェクトにIdentityServer4を実装しました。私はそれを実行し、トークンの要求を送信するために郵便配達を使用した後、URLが存在するにもかかわらず、私は404のステータスコードが見つかりません。IdentityServer4がトークンを返さない - 404が見つかりません

私は基本認証を使用しただけのclient_idを送信していますので、暗黙grant_typeを使用します。

問題も、私はあなたがこのフローを使用するときに渡す必要がある唯一の事はCLIENT_IDと基本認証を使用している理解してきたようにOAuth 2の暗黙の流れのための要求の形式に関連することができます。多分私はそれについて間違っていますか?私はその要求を見ることができるのVisualStudioで

enter image description here

がIdentityServer

enter image description here

に来ても、私は、エラーの種類は404を返して表示されていないデバッグメッセージを見に行けば、私が持っているのは次のとおりです。

Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:44305/baseurl/connect/token 0 
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2017-01-06T11:02:42.0216819Z","tags":{"ai.device.roleInstance":"DESKTOP-3TKHRTV","ai.operation.id":"p4f7oSz6Ng0=","ai.user.userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36","ai.operation.name":"POST /baseurl/connect/token","ai.internal.sdkVersion":"aspnet5c:1.0.0"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"p4f7oSz6Ng0=","name":"POST /baseurl/connect/token","startTime":"2017-01-06T11:02:42.0216819+00:00","duration":"00:00:00.0028138","success":false,"responseCode":"404","url":"https://localhost:44305/baseurl/connect/token","httpMethod":"POST","properties":{"DeveloperMode":"true"}}}} 
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 48.134ms 404 

Code for IdentityServerはかなり簡単で標準的です。

public class Startup 
{ 
     private readonly IHostingEnvironment environment; 

     public Startup(IHostingEnvironment env) 
     { 
      environment = env; 

      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
       .AddEnvironmentVariables(); 

      if (env.IsDevelopment()) 
      { 
       builder.AddApplicationInsightsSettings(developerMode: true); 
      } 

      Configuration = builder.Build(); 
     } 

     public IConfigurationRoot Configuration { get; } 

     public void ConfigureServices(IServiceCollection services) 
     { 
      var cert = new X509Certificate2(Path.Combine(environment.ContentRootPath, "idsvr3test.pfx"), "idsrv3test"); 

      services.AddMvc(); 
      services.AddApplicationInsightsTelemetry(Configuration); 

      services.AddIdentityServer() 
       .AddSigningCredential(cert) 
       .AddInMemoryIdentityResources(ClientConfig.GetIdentityResources()) 
       .AddInMemoryApiResources(ClientConfig.GetApiResources()) 
       .AddInMemoryClients(ClientConfig.GetClients()) 
       .AddInMemoryUsers(ClientConfig.GetUsers()); 
     } 

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      app.UseApplicationInsightsRequestTelemetry(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseBrowserLink(); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      app.UseApplicationInsightsExceptionTelemetry(); 
      app.UseStaticFiles(); 
      app.UseMvcWithDefaultRoute(); 
     } 
} 

public class Program 
{ 
     public static void Main(string[] args) 
     { 
      var host = new WebHostBuilder() 
       .UseKestrel() 
       .UseContentRoot(Directory.GetCurrentDirectory()) 
       .UseIISIntegration() 
       .UseStartup<Startup>() 
       .Build(); 

      host.Run(); 
     } 
} 

public class ClientConfig 
{ 
    public static IEnumerable<Client> GetClients() 
    { 
     return new List<Client> 
     { 
      new Client 
      { 
       ClientId = "mob.client", 
       ClientName = "Mobile client", 
       AllowedGrantTypes = GrantTypes.Implicit, 
       AccessTokenType = AccessTokenType.Jwt, 
       AllowAccessTokensViaBrowser = true, 
       RedirectUris = { "http://localhost:5002/signin-oidc" }, 
       PostLogoutRedirectUris = { "https://localhost:44311/Unauthorized" }, 
       AllowedScopes = new List<string> 
       { 
        IdentityServerConstants.StandardScopes.OpenId, 
        IdentityServerConstants.StandardScopes.Profile 
       } 
      } 
     }; 
    } 

    public static IEnumerable<IdentityResource> GetIdentityResources() 
    { 
     return new List<IdentityResource> 
     { 
      new IdentityResources.OpenId(), 
      new IdentityResources.Profile(), 
     }; 
    } 

    public static IEnumerable<ApiResource> GetApiResources() 
    { 
     return new List<ApiResource> 
     { 
      new ApiResource("MyLegislatureAPI", "BEE MyLegislature API") 
     }; 
    } 

    public static List<InMemoryUser> GetUsers() 
    { 
     return new List<InMemoryUser> 
     { 
      new InMemoryUser{Subject = "818727", Username = "alice", Password = "alice", 
       Claims = new Claim[] 
       { 
        new Claim(JwtClaimTypes.Name, "Alice Smith"), 
        new Claim(JwtClaimTypes.GivenName, "Alice"), 
        new Claim(JwtClaimTypes.FamilyName, "Smith"), 
        new Claim(JwtClaimTypes.Email, "[email protected]"), 
        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean), 
        new Claim(JwtClaimTypes.Role, "Admin"), 
        new Claim(JwtClaimTypes.Role, "Geek"), 
        new Claim(JwtClaimTypes.WebSite, "http://alice.com"), 
        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServerConstants.ClaimValueTypes.Json) 
       } 
      }, 
      new InMemoryUser{Subject = "88421113", Username = "bob", Password = "bob", 
       Claims = new Claim[] 
       { 
        new Claim(JwtClaimTypes.Name, "Bob Smith"), 
        new Claim(JwtClaimTypes.GivenName, "Bob"), 
        new Claim(JwtClaimTypes.FamilyName, "Smith"), 
        new Claim(JwtClaimTypes.Email, "[email protected]"), 
        new Claim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean), 
        new Claim(JwtClaimTypes.Role, "Developer"), 
        new Claim(JwtClaimTypes.Role, "Geek"), 
        new Claim(JwtClaimTypes.WebSite, "http://bob.com"), 
        new Claim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServerConstants.ClaimValueTypes.Json) 
       } 
      } 
     }; 
    } 
} 

誰かが私が間違ってやっていることを見ていますか?

+0

Bodyの下で 'x-www-form-urlencoded'を使用して投稿を試してください。 'username'と 'password'のフォームに2つのキー/値を指定します –

+0

基本認証を使用しますか? –

答えて

4

ここで欠落しているのは、Identity Serverミドルウェアをパイプラインに挿入する部分です。現時点では、DIコンテナに必要なサービスを登録するだけです。あなたのConfigure方法で

、あなたはいくつかの点でapp.UseIdentityServerを呼び出す必要があります。

コア開発者が一緒に入れamazing documentationを見てください。

+0

お返事ありがとうございます。ミドルウェアに不足しているコールを追加した後も、まだ404が見つかりません。しかし、私はあなたが私に提供したこの素晴らしい文書をチェックします。面白いのは、私の同僚は通常、それを実行して、同じコードのために自分のマシンでPostmanを使うときにトークンを得ることができるということです。マシンの構成や設定に関連するものがあるかどうかは確かではありません。少なくとも、私はそう思うはずはありません... @MickaëlDerriey –

+0

@ nemo_87あなたはそうです、そうすべきではありません。あなたとあなたの同僚はまったく同じコードを実行していますか?多分あなたはミドルウェアの順番に違いがありますか? –

+0

コードは全く同じです。私たちはgitを使用しています。したがって、IS4のコードは同じです。同じ設定をしたり、証明書を同じ方法でインストールしたりしていました。私がデバッグモードでIISExpressを使用すると、サーバーが起動して実行されているのがわかるように、Postmanからの呼び出しを受け取りますが、呼び出しが正式なフォーマットではないようです他の人と同じ方法でリクエストしてください:/ @MickaëlDerriey –

3

IdentityServerプロジェクトのURLを確認してください。

私はあなたのパスの一部として "baseurl"を持って参照してください。

地雷は:http://localhost:5000/connect/tokenです。

確認するために、設定URLを確認して問題なく動作することを確認してください。

地雷は:http://localhost:5000/.well-known/openid-configurationです。

関連する問題