2016-09-28 2 views
0

을 감안할 때 : 암시 적 및 자원 소유자 흐름을 지원하는 신원 서버. (구성은 아래 참조)IdentityServer BestPractive는 암시 적 결합 및 RessourceOwner 워크 플로우

IdentityServerBearerTokenAuthentication을 사용하는 Api입니다. 따라서 인증에 사용되는 토큰에 대한 비밀번호를 기본으로 교환합니다.

auth에 암시 적 워크 플로우를 사용합니다.

이제 보호 api 메소드에 액세스 할 수있는 ID 서버에서 베어러 토큰을 가져올 수 있습니다.

또한 사용자로서 암시 적 플로우로 로그인하고 보호 된보기를 볼 수 있습니다.

문제 로그인 한 WebFrontEndUser가 보호 된 API에 액세스하려고 할 때 문제가 발생합니다.

사용자는 암시 적 플로우를 사용하여 ui에 로그인합니다. 인증을받은 후에는 보호 된 API에 액세스하려고 시도합니다. Api가 권한이 없다고 반환합니다.

api가 사용자 쿠키의 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에 액세스 할 수 있습니다. 이렇게하면 별도의 리소스 소유자 클라이언트가 필요하지 않게됩니다.

당신은

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

은 또한 당신의 IdentityServerBearerTokenAuthenticationOptions 토큰은 액세스를 필요로 하나 또는 그 이상의 범위를 명시해야 :, OpenIdConnectAuthenticationOptions으로하여 OpenIdConnectAuthenticationNotifications 속성을 사용 예컨대을이 액세스 토큰을 잡을 수 있습니다. 그렇지 않으면 해당 기관의 모든 액세스 토큰이 API에 액세스 할 수 있습니다. 자세한 내용은 documentation을 참조하십시오.

app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
{ 
    Authority = identServer, 
    RequiredScopes = new[] { "api1" } 
});