在视图中获取ASP.NET身份当前用户 [英] Get ASP.NET Identity Current User In View
问题描述
我使用ASP.NET Identity 2.0和MVC.我需要在视图中记录用户的姓名,姓氏,电子邮件等.怎么能得到呢?我只能得到@ User.Identity,但是没有我的用户类的属性.
I use ASP.NET Identity 2.0 and MVC. I need to logged user's name,surname,email etc.. in view. How can get it? I can get just @User.Identity but there no my user class's property.
//in my view, i need here my ApplicationUser class
<div>
@User.Identity.Name
</div>
//ApplicationUser class
public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole,
CustomUserClaim>
{
public ApplicationUser()
{
this.CreatedDate = DateTime.Now;
}
public DateTime CreatedDate { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string TaxOffice { get; set; }
}
推荐答案
如果仅需要获取特定的属性,则可以将它们作为声明添加到ApplicationUser类中,如以下示例所示:
If there are only specific properties that you need to get, you can add them as claims in your ApplicationUser class like the following example:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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("FullName", this.FullName));
// or use the ClaimTypes enumeration
return userIdentity;
}
这将从Startup.Auth类进行连接:
This gets wired up from the Startup.Auth class:
SessionStateSection sessionStateSection = ConfigurationManager.GetSection("system.web/sessionState") as SessionStateSection;
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/account/login"),
CookieName = sessionStateSection.CookieName + "_Application",
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>
(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => (id.GetUserId<int>())
)
}
});
然后,您可以访问声明(在视图或控制器中):
Then, you can access the claim (in a view or in a controller):
var claims = ((System.Security.Claims.ClaimsIdentity)User.Identity).Claims;
var claim = claims.SingleOrDefault(m => m.Type == "FullName");
这里没有表格身份验证票.
No forms authentication tickets here.
如果您希望获得完整的用户详细信息,则可以始终创建如下扩展方法:
If you want the full user details available, you could always create an extension method like the following:
public static ApplicationUser GetApplicationUser(this System.Security.Principal.IIdentity identity)
{
if (identity.IsAuthenticated)
{
using (var db = new AppContext())
{
var userManager = new ApplicationUserManager(new ApplicationUserStore(db));
return userManager.FindByName(identity.Name);
}
}
else
{
return null;
}
}
并这样称呼它:
@User.Identity.GetApplicationUser();
但是,如果您一直都这样,我建议您进行缓存.
I would recommend caching if you're calling this all this time, however.
这篇关于在视图中获取ASP.NET身份当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!