1. <small id='Rv6RZ'></small><noframes id='Rv6RZ'>

      1. <legend id='Rv6RZ'><style id='Rv6RZ'><dir id='Rv6RZ'><q id='Rv6RZ'></q></dir></style></legend><tfoot id='Rv6RZ'></tfoot>
          <bdo id='Rv6RZ'></bdo><ul id='Rv6RZ'></ul>
      2. <i id='Rv6RZ'><tr id='Rv6RZ'><dt id='Rv6RZ'><q id='Rv6RZ'><span id='Rv6RZ'><b id='Rv6RZ'><form id='Rv6RZ'><ins id='Rv6RZ'></ins><ul id='Rv6RZ'></ul><sub id='Rv6RZ'></sub></form><legend id='Rv6RZ'></legend><bdo id='Rv6RZ'><pre id='Rv6RZ'><center id='Rv6RZ'></center></pre></bdo></b><th id='Rv6RZ'></th></span></q></dt></tr></i><div id='Rv6RZ'><tfoot id='Rv6RZ'></tfoot><dl id='Rv6RZ'><fieldset id='Rv6RZ'></fieldset></dl></div>

        添加自定义声明在 asp.net 核心身份中不起作用

        时间:2023-07-10

          • <tfoot id='7pWPu'></tfoot>
            • <bdo id='7pWPu'></bdo><ul id='7pWPu'></ul>

                <tbody id='7pWPu'></tbody>
            • <legend id='7pWPu'><style id='7pWPu'><dir id='7pWPu'><q id='7pWPu'></q></dir></style></legend>
            • <i id='7pWPu'><tr id='7pWPu'><dt id='7pWPu'><q id='7pWPu'><span id='7pWPu'><b id='7pWPu'><form id='7pWPu'><ins id='7pWPu'></ins><ul id='7pWPu'></ul><sub id='7pWPu'></sub></form><legend id='7pWPu'></legend><bdo id='7pWPu'><pre id='7pWPu'><center id='7pWPu'></center></pre></bdo></b><th id='7pWPu'></th></span></q></dt></tr></i><div id='7pWPu'><tfoot id='7pWPu'></tfoot><dl id='7pWPu'><fieldset id='7pWPu'></fieldset></dl></div>
                1. <small id='7pWPu'></small><noframes id='7pWPu'>

                  本文介绍了添加自定义声明在 asp.net 核心身份中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我创建了 CustomClaimType 来存储 user id:

                  I have created CustomClaimType to store user id:

                  public static class CustomClaimTypes
                  {
                      public const string UserId = "UserId";
                  }
                  

                  当用户登录时,我设置它:

                  When user login, I set it:

                  var claims = new List<Claim>();
                  claims.Add(new Claim(ClaimTypes.Name, doc_session.ufname + " " + doc_session.ulname));
                  claims.Add(new Claim(CustomClaimTypes.UserId, doc_session.isci_id.Value.ToString()));
                  ClaimsIdentity userIdentity = new ClaimsIdentity(claims,"Identity.Application");
                  ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
                  

                  我还创建了自定义身份扩展:

                  I have also created custom identity extension:

                  public static class IdentityExtensions
                  {
                      public static int GetUserId(this IIdentity identity)
                      {
                          ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
                          Claim claim = claimsIdentity?.FindFirst(CustomClaimTypes.UserId);
                  
                          if (claim == null)
                              return 0;
                  
                          return int.Parse(claim.Value);
                      }
                  
                      public static string GetName(this IIdentity identity)
                      {
                          ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
                          Claim claim = claimsIdentity?.FindFirst(ClaimTypes.Name);
                  
                          return claim?.Value ?? string.Empty;
                      }
                  }
                  

                  但是当我调用 User.Identity.GetUserId(); 我得到 0 (null) 结果.

                  But when I call User.Identity.GetUserId(); I get 0 (null) as result.

                  推荐答案

                  要向 Identity 添加额外的声明,您可以实现自定义 CustomClaimsPrincipalFactory.

                  For adding extra claims to Identity, you could implement custom CustomClaimsPrincipalFactory.

                  1. CustomClaimsPrincipalFactory

                  public class CustomClaimsPrincipalFactory : UserClaimsPrincipalFactory<IdentityUser<int>>
                  {
                      public CustomClaimsPrincipalFactory(UserManager<IdentityUser<int>> userManager,
                                                              IOptions<IdentityOptions> optionsAccessor)
                      : base(userManager, optionsAccessor)
                      {
                      }
                  
                      public async override Task<ClaimsPrincipal> CreateAsync(IdentityUser<int> user)
                      {
                          var principal = await base.CreateAsync(user);
                  
                          // Add your claims here
                          ((ClaimsIdentity)principal.Identity).AddClaims(
                              new[] { new Claim(ClaimTypes.Name, user.UserName),
                              new Claim(CustomClaimTypes.UserId, user.Id.ToString())
                              });
                  
                          return principal;
                      }
                  }
                  

                2. 注册CustomClaimsPrincipalFactory

                  public void ConfigureServices(IServiceCollection services)
                  {
                      services.Configure<CookiePolicyOptions>(options =>
                      {
                          // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                          options.CheckConsentNeeded = context => true;
                          options.MinimumSameSitePolicy = SameSiteMode.None;
                      });
                  
                      services.AddDbContext<ApplicationDbContext>(options =>
                          options.UseSqlServer(
                              Configuration.GetConnectionString("DefaultConnection")));
                      services.AddDefaultIdentity<IdentityUser<int>>()
                          .AddEntityFrameworkStores<ApplicationDbContext>();
                      services.AddScoped<IUserClaimsPrincipalFactory<IdentityUser<int>>, CustomClaimsPrincipalFactory>();
                      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
                  }
                  

                3. IdentityExtensions

                  public static class IdentityExtensions
                  {
                      public static int GetUserId(this ClaimsPrincipal identity)
                      {
                          Claim claim = identity?.FindFirst(CustomClaimTypes.UserId);
                  
                          if (claim == null)
                              return 0;
                  
                          return int.Parse(claim.Value);
                      }
                  
                      public static string GetName(this ClaimsPrincipal identity)
                      {
                          Claim claim = identity?.FindFirst(ClaimTypes.Name);
                  
                          return claim?.Value ?? string.Empty;
                      }
                  }
                  

                4. 用途

                5. Useage

                  public IActionResult About()
                  {
                      var claims = User.Claims;
                      var userId = User.GetUserId();
                      var userName = User.GetName();
                      ViewData["Message"] = "Your application description page.";
                  
                      return View();
                  }
                  

                6. 这篇关于添加自定义声明在 asp.net 核心身份中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将现有程序集添加到 mvc 6 项目 下一篇:尝试在 ASP.NET Core MVC 中使用/me/memberOf 时,Microsoft Graph Api 返

                  相关文章

                7. <small id='pb7PB'></small><noframes id='pb7PB'>

                  <tfoot id='pb7PB'></tfoot>
                    <bdo id='pb7PB'></bdo><ul id='pb7PB'></ul>

                  1. <i id='pb7PB'><tr id='pb7PB'><dt id='pb7PB'><q id='pb7PB'><span id='pb7PB'><b id='pb7PB'><form id='pb7PB'><ins id='pb7PB'></ins><ul id='pb7PB'></ul><sub id='pb7PB'></sub></form><legend id='pb7PB'></legend><bdo id='pb7PB'><pre id='pb7PB'><center id='pb7PB'></center></pre></bdo></b><th id='pb7PB'></th></span></q></dt></tr></i><div id='pb7PB'><tfoot id='pb7PB'></tfoot><dl id='pb7PB'><fieldset id='pb7PB'></fieldset></dl></div>
                    <legend id='pb7PB'><style id='pb7PB'><dir id='pb7PB'><q id='pb7PB'></q></dir></style></legend>