Identity Server Windows身份验证声明

时间:2019-11-04 13:05:57

标签: c# asp.net-core identityserver4 windows-authentication

我正在尝试将Identity 4服务器配置为与我的API项目一起使用。目前,我可以请求令牌,但需要在有效负载中添加用户名和角色。我尝试使用IProfileService,但未执行任何操作。如何从Windows身份验证获取此信息?这是我的配置:

launchSettings.json

"iisSettings": {
  "windowsAuthentication": true, 
  "anonymousAuthentication": false 

Program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseKestrel()
            .UseIISIntegration()
            .UseStartup<Startup>();

Startup.cs

        services.Configure<IISOptions>(iis =>
        {
            iis.AutomaticAuthentication = true;
        });

        var builder = services.AddIdentityServer()
              .AddInMemoryIdentityResources(IdentityResourcesConfig.Get())
              .AddInMemoryApiResources(ApiResourcesConfig.Get())
              .AddInMemoryClients(ClientsConfig.Get());

ClientsConfig.cs

        return new Client[]
        {
            new Client
            {
                ClientId = "XYC",
                AllowedGrantTypes = GrantTypes.ClientCredentials,
                AllowedScopes = { "XYC" },
                RequireClientSecret = false,
                AlwaysIncludeUserClaimsInIdToken = true
            }
        };

2 个答案:

答案 0 :(得分:0)

我只使用普通身份验证,但是正在创建和控制将声明与其他应用程序共享的方式的类应该是相同的。

您可能只需要将Claims添加到API资源中,因为默认情况下,客户端使用的声明将不包含在访问令牌中,该令牌也已提供给客户端以请求API。

    public static IEnumerable<ApiResource> GetApis()
    {
        return new ApiResource[]
        {
             new ApiResource("MyApi", "This is my Api name", new List<string> {
                    "mynameclaimclaimname", 

             }),

您在其中添加的索赔名称为“索赔名称”。 如果此方法不起作用,则向我们提供更多信息将很有帮助。如何配置API资源(IdentityServer端和客户端)? 还是您尝试将API配置为客户端?

答案 1 :(得分:0)

第一点是在IdentityServer中,Windows身份验证是一个外部提供程序(与IS本机身份验证cookie相对)。 Windows身份验证是通过使用方案ChallengeAsync在HttpContext上使用Windows API触发的。您可以单击  here了解详情。

另一点是您正在使用客户端凭据流,这在您的方案中是错误的。客户端凭据流使用应用程序的身份,其中没有用户。