微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

已通过身份验证后,如何在 .NET Core 3.1 中添加/更新声明?

如何解决已通过身份验证后,如何在 .NET Core 3.1 中添加/更新声明?

在我的场景中,用户和企业之间存在多对多关系。因此,一个企业可以有很多员工,而一个用户可以是很多企业的员工。

登录页面上,我只想显示电子邮件和密码文本框。一旦他们成功通过身份验证,我想将他们重定向一个页面,其中包含他们受雇的企业的下拉列表。

由于他们已通过身份验证,因此他们的声明已被填充。之后如何添加一个声明(他们的 BusinessID)?

解决方法

来自的答案是指您何时进行身份验证并从 OAUTH 服务器获取声明。我们不知道您使用的是本地身份表还是 OAUTH,但无论如何。

  1. 定义您自己的 UserClaimsPrincipalFactory 类实现
  2. 在启动 ConfigureServices 时注册为服务
  3. 当用户选择业务类型时调用 GenerateClaimsAsync 方法。

我包含了一些旧代码(最终我们以另一种方式实现),但也许可以帮助您。

  1. 定义您自己的 UserClaimsPrincipalFactory。为此,我自定义了 User 类,并添加了一个新工厂
    using System;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Security.Claims;
    using System.Text.Json.Serialization;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.Extensions.Options;
    
    namespace Common.Models.Identity {
        public class User : IdentityUser<int> {
    
    
            public bool SendAlertByEmail { get; set; }
            public int ClientId { get; set; } = Client.DefaultClientId;
            [JsonIgnore,ForeignKey("ClientId")]
            public virtual Client Client { get; set; } = null!;
        }
    
        public class ApplicationUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<User> {
            public ApplicationUserClaimsPrincipalFactory(
                            UserManager<User> userManager,IOptions<IdentityOptions> optionsAccessor)
                            : base(userManager,optionsAccessor) {
            }
    
            protected override async Task<ClaimsIdentity> GenerateClaimsAsync(User user) {
                ClaimsIdentity identity;
    
                identity = await base.GenerateClaimsAsync(user);
                identity.AddClaim(new Claim("ClientId",user.ClientId.ToString()));
                identity.AddClaim(new Claim("ClientDescription",user.Client.Description));
                return identity;
            }
        }
    }
  1. 在你的ConfigureServices中,配置这个

     #region Configure identity
     services
         .AddDefaultIdentity<User>(
             options => {
                 options.SignIn.RequireConfirmedAccount = true;
                 options.Stores.MaxLengthForKeys = 256;            // Max length for key. Regenerate migration if change this
             })
         .AddEntityFrameworkStores<ApplicationDbContext>()
         .AddDefaultUI()
         .AddDefaultTokenProviders()
         .AddClaimsPrincipalFactory<ApplicationUserClaimsPrincipalFactory>();
    
     services.Configure<IdentityOptions>(options => {
         // Password settings.
         options.Password.RequireDigit = true;
         options.Password.RequireLowercase = true;
         options.Password.RequireNonAlphanumeric = true;
         options.Password.RequireUppercase = true;
         options.Password.RequiredLength = 6;
         options.Password.RequiredUniqueChars = 1;
    
         // Lockout settings.
         options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
         options.Lockout.MaxFailedAccessAttempts = 5;
         options.Lockout.AllowedForNewUsers = true;
    
         // User settings.
         options.User.AllowedUserNameCharacters =
         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
         options.User.RequireUniqueEmail = false;
     });
    
     services.ConfigureApplicationCookie(options => {
         // Cookie settings
         options.Cookie.HttpOnly = true;
         options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
    
         options.LoginPath = "/Identity/Account/Login";
         options.AccessDeniedPath = "/Identity/Account/AccessDenied";
         options.SlidingExpiration = true;
     });
     #endregion Configure identity
    
  2. 使用 IoC 将您的 ApplicationUserClaimsPrincipalFactory 传递给您的“选择业务/客户”请求,并将其用于添加声明

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。