如何在没有身份的情况下为控制器创建自己的角色身份验证处理

时间:2019-01-18 15:15:51

标签: c# .net authentication asp.net-core .net-core

我想在.NET Core WEB API应用程序的控制器中实现自己的角色处理。如何为控制器执行此操作将希望支持我自己的角色,例如在Identity中。 我不知道如何实现此功能以在原始身份中使用这样的角色。

我为AppUsers提供了3个表:

  public class AppUser
  {
    public int Id { get; set; }
    public string Username { get; set; }
    public byte[] PasswordHash { get; set; }
    public byte[] PasswordSalt { get; set; }
    public virtual AppUserAddress Address { get; set; }
    public virtual AppUserRole Role { get; set; }
  }

  public class AppUserRole
  {
    public int Id { get; set; }
    public string Name { get; set; }
  }

我想使用这样的角色:

[HttpGet]
[Authorize(Roles = "Admin")]
   public ActionResult<IEnumerable<string>> Get()
   {
       return new string[] { "value1", "value2" };
   }

0 个答案:

没有答案