gpt4 book ai didi

asp.net - 如果在 MVC 3 中未经过身份验证,如何轻松重定向?

转载 作者:行者123 更新时间:2023-12-02 04:43:30 26 4
gpt4 key购买 nike

我是 ASP.NET 的新手,我正在尝试找到一种方法来轻松地将未经身份验证的用户从网站上的任何页面重定向到登录页面。如果有其他选择,我不想将以下代码放入每个 HTTP GET 函数中。

if (!Request.IsAuthenticated)
{
return RedirectToAction("LogOn", "Account");
}

最佳答案

使用[Authorize]属性标记您的 Controller http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx

查看您的 web.config,默认情况下您应该打开 Forms 身份验证 authentication mode="Forms" http://msdn.microsoft.com/en-us/library/eeyk640h.aspx

也看看这个问题ASP.NET MVC Authorization

如果您想要自定义授权行为,请查看此处 Customizing authorization in ASP.NET MVC

关于asp.net - 如果在 MVC 3 中未经过身份验证,如何轻松重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6770563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com