在 ASP.NET MVC 中获取经过身份验证的用户数据
Get Authenticated Identity User data in ASP.NET MVC
我创建了一个 ASP.NET Identity Entity Framework,并在 ASP.NET Identity - MVC
中添加了用于注册和登录功能的客户字段
identityModel.cs
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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
return userIdentity;
}
//add custom fields
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsAdministrator { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Property> Property { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
在另一个名为 属性 的 class 中(登录和身份验证通过后)
我想调用 AspNetUsers table 中的一个字段,这是当前登录的。我想检查 IsAdministrator 字段及其值,并根据该值对要查看的内容执行一些条件.
这是我做过的,但没有成功
public class PropertyController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Property
[Authorize]
public async Task<ActionResult> Index()
{
var user = System.Web.HttpContext.Current.User.Identity.GetUserId();
bool admin = db.Users.SingleOrDefault(y => y.Id.Equals("user")).IsAdministrator;
//return View(await db.Property.ToListAsync());
if (admin == true){
return View(await db.Property.Where(x => x.PropertyID == 1).ToListAsync()); }
else {
return View(await db.Property.ToListAsync());}
}
我能够提取当前登录用户的 UserID,但我想要来自用户的另一个字段或信息,即 IsAdmin 字段,它是 1 或 0。
我怎样才能做到这一点?
这行不通
bool admin = db.Users.SingleOrDefault(y => y.Id.Equals("user")).IsAdministrator;
错误
表达式SingleOrDefault(y => y.Id.Equals("user"))
没有任何意义。它将用 Id = "user"
搜索数据库中的用户,但一无所获。这就是为什么 returns null 导致 NullReferenceException
.
尝试使用以下代码段
...
var userId = System.Web.HttpContext.Current.User.Identity.GetUserName();
var user = UserManager.FindByNameAsync(userId);
var isAdmin = user.IsAdministrator;
...
获取用户
我创建了一个 ASP.NET Identity Entity Framework,并在 ASP.NET Identity - MVC
中添加了用于注册和登录功能的客户字段identityModel.cs
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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
return userIdentity;
}
//add custom fields
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsAdministrator { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Property> Property { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
在另一个名为 属性 的 class 中(登录和身份验证通过后)
我想调用 AspNetUsers table 中的一个字段,这是当前登录的。我想检查 IsAdministrator 字段及其值,并根据该值对要查看的内容执行一些条件.
这是我做过的,但没有成功
public class PropertyController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Property
[Authorize]
public async Task<ActionResult> Index()
{
var user = System.Web.HttpContext.Current.User.Identity.GetUserId();
bool admin = db.Users.SingleOrDefault(y => y.Id.Equals("user")).IsAdministrator;
//return View(await db.Property.ToListAsync());
if (admin == true){
return View(await db.Property.Where(x => x.PropertyID == 1).ToListAsync()); }
else {
return View(await db.Property.ToListAsync());}
}
我能够提取当前登录用户的 UserID,但我想要来自用户的另一个字段或信息,即 IsAdmin 字段,它是 1 或 0。
我怎样才能做到这一点?
这行不通
bool admin = db.Users.SingleOrDefault(y => y.Id.Equals("user")).IsAdministrator;
错误
表达式SingleOrDefault(y => y.Id.Equals("user"))
没有任何意义。它将用 Id = "user"
搜索数据库中的用户,但一无所获。这就是为什么 returns null 导致 NullReferenceException
.
尝试使用以下代码段
...
var userId = System.Web.HttpContext.Current.User.Identity.GetUserName();
var user = UserManager.FindByNameAsync(userId);
var isAdmin = user.IsAdministrator;
...
获取用户