使用授权属性
[Authorize]
public ActionResult MyAction()
{
//stuff
}
您也可以在控制器上使用它。也可以传递用户或角色。
如果你想要更多控制的东西,你可以尝试类似的东西this http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/.
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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)