在ASP.NET MVC中获取经过身份验证的身份用户数据 [英] Get Authenticated Identity User data in ASP.NET MVC

查看:212
本文介绍了在ASP.NET MVC中获取经过身份验证的身份用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个ASP.NET身份实体框架,并在ASP.NET身份-MVC中添加了用于注册和登录功能的客户字段

I Have created an ASP.NET Identity Entity Framework, and added a customer fields for Registration and Login funtions in ASP.NET Identity - MVC

identityModel.cs

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();
        }
    }

在另一个名为Property的类中(通过登录和身份验证之后)

In another class called Property (after login and authentication passed)

我想调用AspNetUsers表中的一个字段,它是当前登录的。我想检查IsAdministrator字段及其值,并根据该值对要查看的内容做一些条件。

I want to call on the one field in AspNetUsers table, which is the currently logged in. I want to check the IsAdministrator field and its values and do some conditions on what to view based on that value.

这是我所做的,但没有成功

this is what I have done, but not successful

 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。

I was able to extract the UserID of the currently logged-in user, but i want the another field or information from the user which is the IsAdmin field which is either 1 OR 0.

我该如何实现?

这不起作用

 bool admin = db.Users.SingleOrDefault(y => y.Id.Equals("user")).IsAdministrator;

错误

推荐答案

表达式 SingleOrDefault(y => y.Id.Equals( user))没有任何意义。它将使用 Id = user 的用户搜索数据库,却一无所获。这就是为什么它返回null导致 NullReferenceException 的原因。

The expression SingleOrDefault(y => y.Id.Equals("user")) does not make any sense. It's going to search database a user with Id = "user" and finds nothing. That's why it returns null which causes the NullReferenceException.

尝试使用以下代码段

...
var userId = System.Web.HttpContext.Current.User.Identity.GetUserName();
var user = UserManager.FindByNameAsync(userId);
var isAdmin = user.IsAdministrator;
...

获取用户

这篇关于在ASP.NET MVC中获取经过身份验证的身份用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆