ASP.NET MVC 授权 [英] ASP.NET MVC Authorization
本文介绍了ASP.NET MVC 授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过MVC asp.net实现授权?
How do I achieve authorization with MVC asp.net?
推荐答案
使用 Authorize 属性
Use the Authorize attribute
[Authorize]
public ActionResult MyAction()
{
//stuff
}
您也可以在控制器上使用它.也可以传入用户或角色.
You can also use this on the controller. Can pass in users or roles too.
如果你想要更多控制的东西,你可以尝试像这个.
If you want something with a little more control, you could try something like this.
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
string[] users = Users.Split(',');
if (!httpContext.User.Identity.IsAuthenticated)
return false;
if (users.Length > 0 &&
!users.Contains(httpContext.User.Identity.Name,
StringComparer.OrdinalIgnoreCase))
return false;
return true;
}
}
这篇关于ASP.NET MVC 授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文