简体   繁体   English

在 ASP.NET MVC 中获取经过身份验证的用户数据

[英]Get Authenticated Identity User data in 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我创建了一个 ASP.NET Identity 实体框架,并在 ASP.NET Identity - MVC 中为注册和登录功能添加了一个客户字段

identityModel.cs身份模型.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();
        }
    }

In another class called Property (after login and authentication passed)在另一个名为 Property 的类中(登录和身份验证通过后)

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

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

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.我能够提取当前登录用户的用户 ID,但我想要来自用户的另一个字段或信息,即 IsAdmin 字段,它要么是 1 要么是 0。

How can i achieve that?我怎样才能做到这一点?

this does not work这不起作用

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

error错误在此处输入图片说明

The expression SingleOrDefault(y => y.Id.Equals("user")) does not make any sense.表达式SingleOrDefault(y => y.Id.Equals("user"))没有任何意义。 It's going to search database a user with Id = "user" and finds nothing.它将在数据库中搜索Id = "user" ,但一无所获。 That's why it returns null which causes the NullReferenceException .这就是它返回 null 导致NullReferenceException原因。

Try to use the following snippet尝试使用以下代码段

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

to get user获取用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC:即使对页面进行身份验证,User.Identity.Name也不可用 - ASP.NET MVC: User.Identity.Name not available even when page is authenticated ASP.NET身份无法识别登录用户是否已通过身份验证 - ASP.NET Identity not recognizing logged in user as authenticated 如何从 asp.net web api 中的身份声明获取 JWT 身份验证的登录用户 - How to get JWT authenticated sign-in user from identity claims in asp.net web api 在ASP.NET身份中获取使用Microsoft帐户进行身份验证的用户的电子邮件 - Get E-mail of User Authenticated with Microsoft Account in ASP.NET Identity 为 ASP.NET MVC 标识创建标识用户页面 - Create an Identity User Page for ASP.NET MVC Identity 如何在ASP.NET MVC View中通过自定义Action Filter对用户进行身份验证? - How to get whether user is authenticated by custom Action Filter in ASP.NET MVC View? 经过身份验证的ASP.NET MVC表单身份验证时重定向用户 - redirect user when Authenticated asp.net mvc forms Authentication 在ASP Classic中从ASP.NET获取经过身份验证的用户 - Get authenticated user from ASP.NET in ASP classic 如何从ASP.NET网页获取SharePoint身份验证的用户? - How to get the SharePoint authenticated user from an ASP.NET webpage? 如何在asp.net中获取Windows身份验证用户的用户名? - How to get the username of windows authenticated user in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM