ASP.NET 身份 - 扩展 User.Identity 的可用字段
ASP.NET Identity - Extend available fields of User.Identity
我很难在我的共享视图中显示 FirstName。请查看我下面的代码,让我知道哪里出错了。
IdentityModels 中的 AppUser class 已扩展为包括 FirstName。
在调试模式下,var claim
为空,我不明白为什么?
IdentityExtensions.cs
public static class IdentityExtensions
{
public static string FirstName(this IPrincipal usr)
{
var claim = ((ClaimsIdentity)usr.Identity).FindFirst("FirstName");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
}
AppUser.cs
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim("FirstName", FirstName));
userIdentity.AddClaim(new Claim("Surname", Surname));
return userIdentity;
}
}
在视图中,我可以看到方法 FirstName(),但是它 returns 是空字符串
_Layout.cshtml
@HttpContext.Current.User.FirstName()
AccountController.cs
public class AccountController : Controller
{
public async Task<ActionResult> SignIn(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
}
只需将您的注册操作编辑为此
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = "MyFirstName" };
您必须在注册操作中设置 FirstName 的值
CreateUserIdentityAsync
不见了。通过添加它,我设法获得了 FirstName 和我添加的其他属性。
public class ApplicationSignInManager : SignInManager<AppUser, string>
{
public override Task<ClaimsIdentity> CreateUserIdentityAsync(AppUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
}
谢谢
我很难在我的共享视图中显示 FirstName。请查看我下面的代码,让我知道哪里出错了。
IdentityModels 中的 AppUser class 已扩展为包括 FirstName。
在调试模式下,var claim
为空,我不明白为什么?
IdentityExtensions.cs
public static class IdentityExtensions
{
public static string FirstName(this IPrincipal usr)
{
var claim = ((ClaimsIdentity)usr.Identity).FindFirst("FirstName");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
}
AppUser.cs
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim("FirstName", FirstName));
userIdentity.AddClaim(new Claim("Surname", Surname));
return userIdentity;
}
}
在视图中,我可以看到方法 FirstName(),但是它 returns 是空字符串 _Layout.cshtml
@HttpContext.Current.User.FirstName()
AccountController.cs
public class AccountController : Controller
{
public async Task<ActionResult> SignIn(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
}
只需将您的注册操作编辑为此
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = "MyFirstName" };
您必须在注册操作中设置 FirstName 的值
CreateUserIdentityAsync
不见了。通过添加它,我设法获得了 FirstName 和我添加的其他属性。
public class ApplicationSignInManager : SignInManager<AppUser, string>
{
public override Task<ClaimsIdentity> CreateUserIdentityAsync(AppUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
}
谢谢