【发布时间】:2014-03-23 17:05:40
【问题描述】:
我正在使用 MVC3、C# 和 Razor。
我正在尝试自定义 Authorize 属性。
一个代码sn-p:
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")]
在自定义属性类中访问此“角色”参数及其值会非常有用,但我不知道该怎么做。 “httpContext”变量中必须有一个属性,但它逃脱了我。
想法?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-3 authorization