我正在使用MVC3,C#和Razor。
我正在尝试自定义Authorize属性。
以下是代码片段:
public class AuthorizeCustomAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var authorized = base.AuthorizeCore(httpContext);
if (!authorized)
{
// The user is not authenticated
return false;
}
var user = httpContext.User;
if (user.IsInRole("Admin")) // This should not be hardcoded, but use the Roles parm somehow. This is the core of my question here.
{
return true;
}
当使用该属性时,会像这样:
[AuthorizeCustom(Roles="Admin,User")]
获取“Roles”参数及其值对于自定义属性类非常有用,但我不知道如何实现。在“httpContext”变量中必须有一个属性,但我无法找到它。
你有什么想法吗?