ASP.NET Web表单获取当前用户的电子邮件并分配用户身份的数据

本文关键字:用户 电子邮件 分配 数据 身份 Web NET 表单 获取 ASP | 更新日期: 2023-09-27 18:01:18

我正在构建一个Web Forms网站,使用ASP身份验证和两个数据库来存储数据。当用户注册时,它在这两个表上创建记录,这是我的寄存器。aspx c#代码:

if (result.Succeeded)
                    {
                        infoLicencia.license_activations++;
                        res_users regUser = new res_users
                        {
                            branch_gkey = infoLicencia.branch_gkey,
                            licence_gkey = infoLicencia.license_gkey,
                            activo = true,
                            fecha_registro = DateTime.Now,
                            email = user.Email
                        };
                        db.res_users.Add(regUser);
                        db.SaveChanges();
                        // Para obtener más información sobre cómo habilitar la confirmación de cuentas y el restablecimiento de contraseña, visite http://go.microsoft.com/fwlink/?LinkID=320771
                        //string code = manager.GenerateEmailConfirmationToken(user.Id);
                        //string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
                        //manager.SendEmail(user.Id, "Confirmar cuenta", "Para confirmar la cuenta, haga clic <a href='"" + callbackUrl + "'">aquí</a>.");
                        signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
                        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    }

这工作得很好,但现在我需要检索存储在另一个数据库上的数据,并使其可供用户会话使用。

我在IdentityModels.cs上试过了:

 public class ApplicationUser : IdentityUser
    {
        public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
        {
            // Tenga en cuenta que authenticationType debe coincidir con el valor definido en CookieAuthenticationOptions.AuthenticationType
            var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Agregar reclamaciones de usuario personalizadas aquí
            var Database2 = new PPublicEntities();
            var UserData = Database2.res_users.Single(i => i.email == Email);
            userIdentity.AddClaim(new Claim("BranchId", UserData.branch_gkey.ToString()));
            return userIdentity;
        }
        public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            return Task.FromResult(GenerateUserIdentity(manager));
        }
    }

但是这个Email属性是空的,我需要当前用户的Email来搜索它在其他数据库上的数据。我以为这个Email属性是在登录时填写的,我真的不知道该怎么做,为什么所有的索赔值都必须是字符串?

ASP.NET Web表单获取当前用户的电子邮件并分配用户身份的数据

默认情况下,在认证期间不将用户电子邮件添加到用户声明中。我们必须显式地将其添加到下面的代码片段中。

userIdentity.AddClaim(new Claim(Constants.EmailClaimType, "User Email", ClaimValueTypes.String));

您还可以编写一个小方法来检索当前用户索赔信息。电子邮件:

public static string GetCurrentUserEmail()
        {
            var userEmail = string.Empty;
            var claimIdentity = new ClaimsIdentity(Thread.CurrentPrincipal.Identity);
            var userEmailClaim = claimIdentity.Claims.FirstOrDefault(x => x.Type == IdentityConstants.EmailClaimType);
            if (userEmailClaim != null)
            {
                userEmail = userEmailClaim.Value;
            }
            return userEmail;
        }

你可能想看看我创建的一个库:GitHub