6 c# asp.net-mvc attributes authorization custom-attributes
好吧,我是C#的新手,我正在尝试使用ASP MVC2创建一个小网站.
我想创建自己的授权属性.但如果可能,我需要传递一些值.
例如:
[CustomAuthorize(GroupID = Method Parameter?]
public ActionResult DoSomething(int GroupID)
{
return View("");
}
Run Code Online (Sandbox Code Playgroud)
我想授权访问页面.但它取决于传递给控制器的值.因此授权取决于groupID.这有可能以任何方式实现吗?
提前致谢.
使用价值提供者:
public class CustomAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
var result = filterContext.Controller.ValueProvider.GetValue("GroupId"); //groupId should be of type `ValueProviderResult`
if (result != null)
{
int groupId = int.Parse(result.AttemptedValue);
//Authorize the user using the groupId
}
}
Run Code Online (Sandbox Code Playgroud)
}
这篇文章可能会对您有所帮助。
HTH,
查尔斯
| 归档时间: |
|
| 查看次数: |
3120 次 |
| 最近记录: |