首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IdentityServer BestPractive结合隐式工作流和RessourceOwner工作流

IdentityServer BestPractive结合隐式工作流和RessourceOwner工作流
EN

Stack Overflow用户
提问于 2016-09-28 13:04:24
回答 1查看 459关注 0票数 0

and:A Identity Server,它支持隐式和资源所有者流。(配置见下文)

Api,它使用IdentityServerBearerTokenAuthentication。因此,它基本地将一个密码交换为一个用于auth的令牌。

一个UI,它为auth使用隐式工作流。

现在,我可以从身份服务器获得一个承载令牌,用它可以访问受保护的api方法。

另外,作为一个用户,我能够使用隐式流登录并查看受保护的视图。

问题当在WebFrontEndUser中签名的用户想要访问受保护的API时,就会出现问题。

用户在ui中使用隐式流登录。经过身份验证后,他尝试访问受保护的api。阿皮返回说他没有被授权。

如何配置环境,使api使用来自用户cookie的openid信息?

网站Confing

代码语言:javascript
复制
   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

代码语言:javascript
复制
app.UseIdentityServerBearerTokenAuthentication(new IdentityServer3.AccessTokenValidation.IdentityServerBearerTokenAuthenticationOptions()
        {
            Authority = identServer
        });

IdentityServer Client Config

代码语言:javascript
复制
  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"
                    }
                }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-28 15:16:33

使用OpenID连接,您可以在身份验证期间请求标识和访问令牌。您已经在身份验证期间获得了一个访问令牌(响应类型为token),因此我将获取该令牌并使用它访问API。这将避免需要单独的资源所有者客户端。

您可以使用OpenIdConnectAuthenticationNotifications属性OpenIdConnectAuthenticationOptions获取此访问令牌,例如:

代码语言:javascript
复制
Notifications = new OpenIdConnectAuthenticationNotifications
{
    SecurityTokenValidated = x =>
    {
        x.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", x.ProtocolMessage.AccessToken));
        return Task.FromResult(0);
    }
}

另外,您的IdentityServerBearerTokenAuthenticationOptions应该声明一个或多个令牌访问所需的Scopes。否则,来自该权限的任何访问令牌都可以访问您的API。有关此问题的更多细节,请参见文档

代码语言:javascript
复制
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
    Authority = identServer,
    RequiredScopes = new[] { "api1" }
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39748414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档