【发布时间】:2015-11-21 16:00:50
【问题描述】:
我在尝试使用标准 Authorize 属性时遇到问题。似乎只是被忽略了。我试图解决这个问题是编写自己的,它被调用但参数没有被传递。
BasicAuthorizeAttribute 的默认构造函数被调用,但接受字符串参数的构造函数从未被调用。也调用了 OnAuthorization,但从未设置 Roles 属性。
尽管在 web.config 中将身份验证设置为无,但我们正在使用 Windows 身份验证。将其更改为 Windows 没有任何区别。
我们正在使用 MVC 5 和 Castle Windsor,我怀疑这会导致我的问题。
关于控制器以及我的操作:
[BasicAuthorize(Roles = "Developer")]
属性过滤器
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class BasicAuthorizeAttribute : ActionFilterAttribute, IAuthorizationFilter
{
public string Roles { get; set; }
public BasicAuthorizeAttribute()
{
}
public BasicAuthorizeAttribute(string roles)
{
Roles = roles;
}
public void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
{
if (!string.IsNullOrWhiteSpace(Roles))
{
// Check user roles here
// User not in role
filterContext.Result = new HttpUnauthorizedResult(); // mark unauthorized
}
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
var user = filterContext.HttpContext.User;
if (user == null || !user.Identity.IsAuthenticated)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
温莎城堡的设置如下。
...
Component.For<FilterAttribute>().ImplementedBy<BasicAuthorizeAttribute>().LifeStyle.PerWebRequest.Named(typeof(BasicAuthorizeAttribute).FullName)
...
在 web.config 我们有以下内容
...
<authentication mode="None" />
...
<modules>
<remove name="FormsAuthenticationModule" />
</modules>
...
我花了一天多的时间在这上面,Google 不再是我的朋友了。有人对如何解决这个问题有建议吗?还是最好使用默认的[Authorize(Roles = "...")] 属性?
编辑
我找到了罪魁祸首。以下几行导致了问题。
var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
FilterProviders.Providers.Remove(oldProvider);
【问题讨论】:
-
您使用的是默认动作调用程序吗? Windsor 不会创建您的属性。动作调用者将
-
我根本不认识温莎。我如何知道它是否使用了默认的动作调用程序?正在调用该属性,断点被命中。它只是没有传递参数。
-
温莎这里根本不涉及
-
如果我删除 Windsor 的“Component.For
()...”代码,那么我的属性将被完全忽略并且断点不会命中 -
拆除温莎城堡后一切正常。所以这一定是设置错误的地方。感谢您的帮助。
标签: asp.net-mvc-5 castle-windsor custom-attributes