2016-09-28 2 views
0

を考える: 暗黙的およびリソース所有者の流れをサポートアイデンティティ・サーバー。 (構成は下記参照)IdentityServer BestPractiveは暗黙の組み合わせとRessourceOwnerワークフロー

IdentityServerBearerTokenAuthenticationを使用するApi。したがって、認証に使用されるトークンのパスワードを基本的に取引します。

authの暗黙のワークフローを使用します。

これで、保護されたapiメソッドにアクセスできるIDサーバからベアラトークンを取得できました。

また、ユーザーとして、暗黙のフローでログインし、保護されたビューを見ることができます。

問題 署名されたWebFrontEndUserが保護されたAPIにアクセスするときに問題が発生します。

ユーザは、暗黙のフローを使用してuiにログインします。彼は認証された後、保護されたAPIにアクセスしようとします。 Apiは、彼が承認されていないことを返します。

apiがユーザーCookieのopenid情報を使用するように環境を設定する方法を教えてください。

サイトConfing

app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      }); 

      app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
      { 
       ClientId = "foo_implicit", 
       Authority = identServer, 
       RedirectUri = "http://localhost/foo/", 
       ResponseType = "token id_token", 
       Scope = "openid profile", 
       SignInAsAuthenticationType = DefaultAuthenticationTypes.ApplicationCookie 
      }); 

WEBAPIコンフィグ

app.UseIdentityServerBearerTokenAuthentication(new IdentityServer3.AccessTokenValidation.IdentityServerBearerTokenAuthenticationOptions() 
     { 
      Authority = identServer 
     }); 

IdentityServerクライアントコンフィグ

new Client 
       { 
        Enabled = true, 
        ClientId = "foo_implicit", 
        ClientName = "foo Site", 
        ClientSecrets = new List<Secret> 
        { 
         new Secret("foo".Sha256()) 
        }, 
        Flow = Flows.Implicit, 
        AllowedScopes = new List<string> 
        { 
         Constants.StandardScopes.OpenId, 
         Constants.StandardScopes.Profile, 
         "read" 
        }, 
        RedirectUris = new List<string>() 
        { 
         "http://localhost/foo/" 
        } 
       }, 
       new Client 
       { 
        Enabled = true, 
        ClientId = "foo", 
        ClientName = "foo api", 
        ClientSecrets = new List<Secret> 
        { 
         new Secret("foo".Sha256()) 
        }, 
        Flow = Flows.ResourceOwner, 
        AllowedScopes = new List<string> 
        { 
         Constants.StandardScopes.OpenId, 
         "read" 
        } 
       } 

答えて

4

U OpenID Connectを使用すると、認証中にIDとアクセストークンの両方を要求できます。認証(応答タイプtoken)中にすでにアクセストークンが取得されているため、これを取得してAPIにアクセスするために使用します。これにより、別個のリソースオーナークライアントが不要になります。

あなたはOpenIdConnectAuthenticationOptions、例えばのOpenIdConnectAuthenticationNotificationsプロパティを使用して、このアクセストークンをつかむことができます。:

Notifications = new OpenIdConnectAuthenticationNotifications 
{ 
    SecurityTokenValidated = x => 
    { 
     x.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", x.ProtocolMessage.AccessToken)); 
     return Task.FromResult(0); 
    } 
} 

また、あなたのIdentityServerBearerTokenAuthenticationOptionsは、トークンがアクセスするために必要となる、1つまたは複数のスコープを明記してください。そうしないと、その当局のアクセストークンからAPIにアクセスできます。詳細については、documentationを参照してください。

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
{ 
    Authority = identServer, 
    RequiredScopes = new[] { "api1" } 
}); 
関連する問題