2017-03-24 14 views
2

ヘルプ、私はイオンフロントエンドで.NET Core APIを構築しています。私は、私は多かれ少なかれ、この例で、次のここ https://identityserver4.readthedocs.io/en/release/quickstarts/6_aspnet_identity.htmlIdentityServer4 invalid_token Azureで「発行者が無効です」、ローカルホストで対応

は私がStartup.cs

// Adds IdentityServer 
services.AddIdentityServer()     
    .AddTemporarySigningCredential() 
    .AddInMemoryIdentityResources(Config.GetIdentityResources()) 
    .AddInMemoryApiResources(Config.GetApiResources()) 
    .AddInMemoryClients(Config.GetClients(Configuration)) 
    .AddAspNetIdentity<ApplicationUser>(); 

app.UseIdentity(); 
app.UseIdentityServer(); 
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
{ 
    Authority = API_address, 
    RequireHttpsMetadata = false, 

    ApiName = "myAPIs" 
}); 

、私のConfigでに持っているものであるたASPNETコアアイデンティティを使用したいですメモリ構成の.csファイルです。

public class Config 
{ 
    // scopes define the resources in your system 
    public static IEnumerable<IdentityResource> GetIdentityResources() 
    { 
     return new List<IdentityResource> 
     { 
      new IdentityResources.OpenId(), 
      new IdentityResources.Profile(), 
     }; 
    } 

    // scopes define the API resources in your system 
    public static IEnumerable<ApiResource> GetApiResources() 
    { 
     return new List<ApiResource> 
     { 
      new ApiResource(
       "myAPIs",          // Api resource name 
       "My API Set #1",        // Display name 
       new[] { JwtClaimTypes.Name, JwtClaimTypes.Role }) // Claims to be included in access token 
     }; 
    } 

    // client want to access resources (aka scopes) 
    public static IEnumerable<Client> GetClients(IConfigurationRoot configuration) 
    { 
     return new List<Client> 
     { 
      new Client 
      { 
       ClientId = "myClient", 
       ClientName = "My Custom Client", 
       AllowedCorsOrigins = new List<string> 
       { 
        "whateverINeedHere" 
       }, 
       AccessTokenLifetime = 60 * 60 * 24, 
       AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, 
       RequireClientSecret = false, 
       AccessTokenType = AccessTokenType.Jwt, 
       AllowedScopes = 
       { 
        "myAPIs" 
       } 
      } 
     }; 
    } 
} 

問題は、これをローカルでテストするとすべてうまく動作するということです。 私は/ connect/tokenエンドポイントに到達しました。私はトークンレスポンスを取得し、トークン認可を必要とするコントローラにヒットし、私のクレームはそこにあります。しかし、Azureにデプロイするときに、その環境から発行されたトークンを使用したい場合、私は401ヘッダーのinvalid_tokenを "Unsuhorized"とします。 "発行者は無効です"私はグーグルではありますが、発行者ではなく署名の問題で無効なトークンを取得します。以前はアイデンティティ・サーバーを使ったことが一度もありませんでしたが、これは構成上の問題のようです。私はjwt.ioのアイデンティティサーバーから得たトークンを比較しました。それらはまったく同じように見えますが、唯一の違いは発行者のlocalhost - > myAPIAddressです。

誰かが正しい方向に向かうことができますか?

答えて

0

クライアントとIdentityServerの間にSSL/TLSの問題がありますか?IdentityServer自体からログに記録された例外を表示できますか?

"... Could not establish trust relationship for the SSL/TLS..." 

HTTPSでIdentityServerを実行している場合、証明書にそのドメイン/サブドメインがあることを確認する必要があります。

どちらの方法でも、IdentityServerは多くの有用な情報をログに記録し、ロギングをオンにして、それが正しい方向に向いていることを確認します。

+0

こんにちはマット、あなたの答えをありがとう。はい、私はHTTPS上ですべてを実行しており、私はここで指示に従って設定しています(Serilogの代わりにLogglyを使用しています) {"IdentityServer"、LogLevel.Debug} IdentityServerからデバッグ情報は表示されませんでした。私のコードから他のデバッグ情報が表示されます。 –

+0

OK、IdentityServerが実行されているドメインまたはサブドメインは、証明書の共通名または代替名ですか? – Matt

0

これは一時的な署名資格情報である可能性があるので、このような匂いがします。私は証明書がロードされていないときにAzureにデプロイするときに問題に遭遇しました。

次の手順を使用して、自己署名付き証明書を作成し、それを青信号に追加することをお勧めします。 (これは新しいポータルで行うことができます)。 https://azure.microsoft.com/en-us/blog/using-certificates-in-azure-websites-applications/

覚えている:あなたは WEBSITE_LOAD_CERTIFICATESアプリケーションの設定を追加していることを確認してください!

また、私のstartup.csにcertをロードするために使用するコードもあります。私はレポジトリに証明書のコピーを保管しておくので、フォールバックとしてディスクから読み込むことができます(私が開発マシンにいるとき)。

X509Certificate2 cert = null; 
using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser)) 
{ 
    certStore.Open(OpenFlags.ReadOnly); 
    X509Certificate2Collection certCollection = certStore.Certificates.Find(
     X509FindType.FindByThumbprint, 
     // Replace below with your cert's thumbprint 
     "A9781679661914B7539BE020EE9C4F6880579F42", 
     false); 
    // Get the first cert with the thumbprint 
    if (certCollection.Count > 0) 
    { 
     cert = certCollection[0]; 
     // Use certificate 
     Log.Logger.Information($"Successfully loaded cert from registry: {cert.FriendlyName}"); 
    } 
} 

// Fallback to local file for development 
if (cert == null) 
{ 
    cert = new X509Certificate2(Path.Combine(_env.ContentRootPath, "myauth.pfx"), "mypassword"); 
    Log.Logger.Information($"Falling back to cert from file. Successfully loaded : {cert.FriendlyName}"); 
} 

services.AddIdentityServer() 
    .AddSigningCredential(cert) 
+0

答えをありがとう。これは現在、私の優先順位のリストにはありませんが、すぐに新しいデプロイメントを試してみて、これが役立つならあなたに連絡します。 –

関連する問題