如果MVC 3未通过身份验证如何轻松地重定向? [英] How to easily redirect if not authenticated in MVC 3?
问题描述
我的品牌新的ASP.NET,我试图找到一种方法可以轻松地重定向从任何页面未认证用户的网站登录页上。我想preFER不把下面的code在每一个HTTP GET功能,如果有另一种选择。
如果(!Request.IsAuthenticated)
{
返回RedirectToAction(登录,帐户);
}
标记与控制器[授权]
属性<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx\">http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx
请参阅你的web.config,默认情况下你应该窗体身份验证打开验证模式=表格
http://msdn.microsoft.com/en-us/library/eeyk640h.aspx
另外,也要看看这个问题, ASP.NET MVC授权
在情况下,如果你想拥有定制的授权行为看这里的Customizing在ASP.NET MVC 授权
I am brand new to ASP.NET, and I'm trying to find a way to easily redirect an unauthenticated user from any page on the site to the logon page. I would prefer to not put the following code in every HTTP GET function if there is another option.
if (!Request.IsAuthenticated)
{
return RedirectToAction("LogOn", "Account");
}
Mark your controller with [Authorize]
attribute http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx
See your web.config, by default you should have Forms authentication turned on authentication mode="Forms"
http://msdn.microsoft.com/en-us/library/eeyk640h.aspx
Also look at this question ASP.NET MVC Authorization
In case if you want to have custom Authorize behavior look here Customizing authorization in ASP.NET MVC
这篇关于如果MVC 3未通过身份验证如何轻松地重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!