c# - 不退还所有 claim 和保单授权问题

标签 c# identityserver4 asp.net-core-2.0

我正在使用 IdentityServer4 混合流示例应用程序来测试和使用 IS4:https://github.com/IdentityServer/IdentityServer4.Samples/tree/release/Quickstarts/5_HybridFlowAuthenticationWithApiAccess

IS4 中的 Config.cs 有两个定义的测试用户:

return new List<TestUser>
        {
            new TestUser
            {
                SubjectId = "1",
                Username = "alice",
                Password = "password",

                Claims = new List<Claim>
                {
                    new Claim("name", "Alice"),
                    new Claim("website", "https://alice.com")
                }
            },
            new TestUser
            {
                SubjectId = "2",
                Username = "bob",
                Password = "password",

                Claims = new List<Claim>
                {
                    new Claim("name", "Bob"),
                    new Claim("website", "https://bob.com")
                }
            }
        };

MVC 客户端中的“Secure.cshtml”应该显示所有声明的类型和值,但我只看到了

sid: ec1e3b0513f711ca6c29a90494aa9741
sub: 1
idp:local
name:Alice

没有显示“网站”声明。我怎样才能让这个声明连同名字一起回来? options.GetClaimsFromUserInfoEndpoint 设置为“true”。 (MVC 客户端 Startup.cs)

所以,如果我创建一个策略:

services.AddAuthorization(options => {
                options.AddPolicy("testPolicy", builder =>
                {
                    builder.RequireAuthenticatedUser();
                    builder.RequireClaim("website", "https://alice.com");
                });
            });

并用它保护“安全”操作:

[Authorize(Policy = "testPolicy")]
        public IActionResult Secure()
        {
            ViewData["Message"] = "Secure page.";

            return View();
        }

我无法访问它,因为“网站”声明未从用户端点返回...有什么想法吗? 注意:除了“testPolicy”之外,我没有对示例应用程序进行任何更改。

最佳答案

我记得您需要将 UserClaims 添加到您的 ApiResource:

    public static IEnumerable<ApiResource> GetApiResources()
    {
        return new List<ApiResource>
        {
            new ApiResource("api1.access")
            {
                UserClaims = new List<string>
                {
                    JwtClaimTypes.Name, // or just put "name"
                    JwtClaimTypes.WebSite // or just put "website" 
                },
                Scopes = new List<Scope>
                {
                    new Scope("api1.access")
                }
            }
        };
    }

关于c# - 不退还所有 claim 和保单授权问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47297129/

相关文章:

.net - IClrTypeMapping 与 ClrTypeMappingDescriptor

asp.net-identity - ASP.NET Core 2.0 上的GenerateChangePhoneNumberTokenAsync

C# 从 XML 中提取数据

kubernetes - Sitecore 10 K8S 部署 - 无效的 redirect_uri 和 key 集缺失错误

sql-server - 具有 Azure SQL 托管标识的 Entity Framework Core 导致用户“NT AUTHORITY\ANONYMOUS LOGON 登录失败”

angular - 如何在身份服务器身份验证 oidc-client Angular 后关闭 signinPopup() 窗口

c# - 无法使用 C# 在 Selenium WebDriver 中使用现有的 Firefox 配置文件

c# - 使用 System.Net.Mail lib 发送的电子邮件显示中文字母

c# - FormUrlEncodedContent 和查询字符串有什么区别?

c# - 使用带有版本开关的 Get/Set 来定义属性的最有效方法是什么?