使用[Authorize]而不使用SimpleMembershipProvider [英] Using [Authorize] without SimpleMembershipProvider

查看:131
本文介绍了使用[Authorize]而不使用SimpleMembershipProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用...

Is there a way to use...

[Authorize(Roles: "Administrator")]
public class SomeController : Controller
{
    ...
}

...使用我自己的角色数据库表,而不使用 SimpleMembershipProvider

...with my own Roles database table, without using SimpleMembershipProvider?

我的用户角色模型类:

[Table("Users")]
public class UserModel
{
    [Key]
    public Int32 ID { get; set; }

    [Required]
    public String Name { get; set; }

    [Required]
    public String Password { get; set; }

    [Required]
    public virtual RoleModel Role { get; set; }
}

[Table("Roles")]
public class RoleModel
{
    [Key]
    public Int32 ID { get; set; }

    [Required]
    public String Name { get; set; }

    public virtual ICollection<UserModel> Users { get; set; }
}

有人有同样的问题吗?

推荐答案

您应该通过继承 AuthorizeAttribute 授权 c $ c> class

You should create your own Authorize attribute by inheriting from AuthorizeAttribute class

public class CustomAuthorizeAttribute : AuthorizeAttribute
{

}

然后你可以配置它,但是你喜欢。

Then you can configure it however you like.

另外您可以在Stackoverflow上查看这些问题:

Also you can take a look at these questions on Stackoverflow:


  1. 自定义授权属性

  2. ASP.NET MVC 4自定义授权带权限代码的属性(不含角色)

  1. Custom Authorize Attribute
  2. ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

这篇关于使用[Authorize]而不使用SimpleMembershipProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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