C# ASP.NET MVC 授权
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/329658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Authorization
提问by Tablet
How do I achieve authorization with MVC asp.net?
如何使用MVC asp.net实现授权?
采纳答案by Dan
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;
}
}
回答by MrJavaGuy
There is an Authorization feature with MVC, using ASP.NET MVC beta and creating the MVC project from Visual Studio, automatically adds a controller that used authorization. One thing that will help with your google search, is that it is a "filter". So try searching on "Authorization Filter MVC" and anything preview 4 or greater will help.
MVC 有一个授权功能,使用 ASP.NET MVC beta 并从 Visual Studio 创建 MVC 项目,自动添加使用授权的控制器。对您的谷歌搜索有帮助的一件事是,它是一个“过滤器”。因此,尝试搜索“授权过滤器 MVC”,任何预览 4 或更高版本都会有所帮助。
回答by Dmitry
I would recommend to take a look at this article: http://kbochevski.blogspot.com/2009/11/mvc-forms-authentication-and.html
我建议看看这篇文章:http: //kbochevski.blogspot.com/2009/11/mvc-forms-authentication-and.html
It helped me today.
它今天帮助了我。
回答by George Kosmidis
This is how you can have authentication by default: http://mycodepad.wordpress.com/2014/03/17/mvc-secure-your-web-app/
这是默认情况下您可以进行身份验证的方式:http: //mycodepad.wordpress.com/2014/03/17/mvc-secure-your-web-app/