如何在没有ASP.NET身份的情况下使用Authorize

问题描述 投票:0回答:1

在我的项目中使用没有Asp.net身份的身份我正在登录部分,我想使用[Authorize(Role =“...”)]但我不知道该怎么做。

请帮我用

StartUp.cs:

services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

            }).AddCookie(options =>
            {
                options.LoginPath = "/Account/Login";
                options.LogoutPath = "/Account/Logout";
                options.ExpireTimeSpan = TimeSpan.FromMinutes(5000);
            });

登录操作

if (ModelState.IsValid)
            {
                User user = new User();

                user = _iuser.LoginUser(login.Mobile, login.Password);

                if (user != null)
                {
                    if (user.IsActive)
                    {
                        var claims = new List<Claim>()
                        {
                            new Claim(ClaimTypes.NameIdentifier,user.Id.ToString()),
                            new Claim(ClaimTypes.Name,user.Mobile)
                        };
                        var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                        var principal = new ClaimsPrincipal(identity);

                        var properties = new AuthenticationProperties()
                        {
                            IsPersistent = login.IsRemember
                        };

                        HttpContext.SignInAsync(principal, properties);
                        return RedirectToAction("Index", "Profile");

                    }
                    else
                    {
                        return RedirectToAction(nameof(Active));
                    }

用户类:

public class User
    {
        [Key]
        public int Id { get; set; }
        public int RoleId { get; set; }
        public string Mobile { get; set; }
        public string Password { get; set; }
        public string Code { get; set; }
        public bool IsActive { get; set; }
        [ForeignKey("RoleId")]
        public virtual Role Role { get; set; }
    }

角色类:

public class Role
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public virtual ICollection<User> Users { get; set; }
    }

ApplicationDbContext:

public class ApplicationDbContext: DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }

        public DbSet<Role> Roles { get; set; }
        public DbSet<User> Users { get; set; }
    }

提示:我正在使用ASP.NET CORE 2.2和Entity Frame Work Core 2.2

asp.net-core identity
1个回答
0
投票

您的问题有两种解决方案:

  1. 在声明列表中添加Role Claim
claims.Add(new Claim(ClaimTypes.Role, "Admin"));
  1. 创建custom authorization
© www.soinside.com 2019 - 2024. All rights reserved.