and:A Identity Server,它支持隐式和资源所有者流。(配置见下文)
Api,它使用IdentityServerBearerTokenAuthentication。因此,它基本地将一个密码交换为一个用于auth的令牌。
一个UI,它为auth使用隐式工作流。
现在,我可以从身份服务器获得一个承载令牌,用它可以访问受保护的api方法。
另外,作为一个用户,我能够使用隐式流登录并查看受保护的视图。
问题当在WebFrontEndUser中签名的用户想要访问受保护的API时,就会出现问题。
用户在ui中使用隐式流登录。经过身份验证后,他尝试访问受保护的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 Config
app.UseIdentityServerBearerTokenAuthentication(new IdentityServer3.AccessTokenValidation.IdentityServerBearerTokenAuthenticationOptions()
{
Authority = identServer
});IdentityServer Client Config
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"
}
}发布于 2016-09-28 15:16:33
使用OpenID连接,您可以在身份验证期间请求标识和访问令牌。您已经在身份验证期间获得了一个访问令牌(响应类型为token),因此我将获取该令牌并使用它访问API。这将避免需要单独的资源所有者客户端。
您可以使用OpenIdConnectAuthenticationNotifications属性OpenIdConnectAuthenticationOptions获取此访问令牌,例如:
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = x =>
{
x.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", x.ProtocolMessage.AccessToken));
return Task.FromResult(0);
}
}另外,您的IdentityServerBearerTokenAuthenticationOptions应该声明一个或多个令牌访问所需的Scopes。否则,来自该权限的任何访问令牌都可以访问您的API。有关此问题的更多细节,请参见文档。
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = identServer,
RequiredScopes = new[] { "api1" }
});https://stackoverflow.com/questions/39748414
复制相似问题