【问题标题】:IdentityServer4 with Windows authentication and custom claims具有 Windows 身份验证和自定义声明的 IdentityServer4
【发布时间】:2018-06-04 14:44:41
【问题描述】:

我对 Windows 身份验证和自定义声明有疑问。
我有一个带有 windows auth 的 identityServer,并且 User.Identity.Name 显示我的 AD 名称。 但我不明白,我应该如何从我的存储中添加一些属性给这个用户。我现在有这样的东西:

var claims = new List<Claim> {
      new Claim("devhomepage", "www.devsite.com", ClaimValueTypes.String)};
var userIdentity = new ClaimsIdentity(claims, "siteinfo");
User.AddIdentity(userIdentity);
await HttpContext.SignInAsync(User);
return RedirectToLocal(returnUrl);

它不起作用 :-) 我的客户将没有被授权。

这是服务器的配置

new Client
                {
                    ClientId = "mvc",
                    ClientName = "MVC Client",
                    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    RedirectUris = {"http://localhost:60640/signin-oidc"},// where to redirect to after login
                    PostLogoutRedirectUris = {"http://localhost:60640/signout-callback-oidc"},// where to redirect to after logout
                    AllowedScopes = new List<string>
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                    },
                    AllowOfflineAccess = true,
                    RequireConsent = false

并且不是客户

services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies")
                .AddOpenIdConnect("oidc", options =>
                {
                    options.SignInScheme = "Cookies";
                    options.Authority = "http://localhost:49245/";
                    options.RequireHttpsMetadata = false;
                    options.ClientId = "mvc";
                    options.ClientSecret = "secret";
                    options.ResponseType = "code id_token";
                    options.GetClaimsFromUserInfoEndpoint = true;
                    options.SaveTokens = true;
                });

【问题讨论】:

    标签: identityserver4


    【解决方案1】:

    对于登录,我必须使用 Microsoft.AspNetCore.Identity.SignInManager 类和方法 SignInAsync(/*我应该租用一个 TUser 对象,该对象存储在我的数据库中,并与我的 AD 帐户进行映射 */)

    使用自定义声明(将用于 IS4 中具有自定义声明的所有示例):

    public class ProfileService : IProfileService
        {
            protected UserManager<IdentityUser> _userManager;
    
            public ProfileService(UserManager<IdentityUser> userManager)
            {
                _userManager = userManager;
            }
    
            public async Task GetProfileDataAsync(ProfileDataRequestContext context)
            {
                //>Processing
                var user = await _userManager.GetUserAsync(context.Subject);
                //my custom claims
                var claims = new List<Claim>
                    {
                        new Claim("devhomepage", "www"),
                        new Claim("reporting","reps")
                    };
    
                context.IssuedClaims.AddRange(claims);
            }
    
            public async Task IsActiveAsync(IsActiveContext context)
            {
                //>Processing
                var user = await _userManager.GetUserAsync(context.Subject);
    
                context.IsActive = (user != null);
            }
        }
    

    但是这个服务我必须在我的 IdentityService 之后注册

    services.AddIdentityServer()
    ...
    services.AddTransient<IProfileService, ProfileService>();
    }
    

    配置服务

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      相关资源
      最近更新 更多