FormsAuthentication.RedirectToLoginPage()VS RedirectToAction(QUOT;登录","帐户]) [英] FormsAuthentication.RedirectToLoginPage() vs RedirectToAction("Login", "Account")
问题描述
我用我的网站表单验证。我曾经在一些例如code看到,人们可以称.SignOut(),然后使用
FormsAuthentication.RedirectToLoginPage()
到用户发送到登录页面。
如果有的话,这有超过调用什么优势,
RedirectToAction(登录,帐户);
在MVC网站?从MSDN似乎前者不会叫的Htt presponse.End(),这意味着随后将执行code ...我不知道,当我需要使用此功能。
FormsAuthentication.RedirectToLoginPage()
确实有一定的优势,它会尝试追加?RETURNURL = {URL}
来以后它可以用来返回用户,他们在登录重定向发生被请求的页面的登录页面的URL。使用这种方法使用的Response.Redirect()
这违背了MVC的心态。你会从你的控制器/过滤器射击失去了像 OnActionExecuting
事件。 RedirectToLoginPage
RedirectToAction(登录,帐户);
不具有 RETURNURL
功能出来的框,但它确实把一切都在MVC的生态系统使事件触发。在这种情况下,如果记录了一个人,重定向回到登录页面,我可能会保持它的所有MVC中,并使用 RedirectToAction
。或可能使用重定向(FormsAuthentication.Url);
如果你总是希望使用的登录页面URL从web.config
I am using Forms Authentication in my website. I have seen in some example code that one can call .SignOut() and then use
FormsAuthentication.RedirectToLoginPage()
to send a user to the login page.
What advantage, if any, does this have over calling
RedirectToAction("Login", "Account");
in an MVC website? From MSDN it seems that the former will not call HttpResponse.End() which means that code that follows will execute... I'm not sure when I would need to use this feature.
FormsAuthentication.RedirectToLoginPage()
does have some advantage where it will attempt to append ?ReturnUrl={url}
to the login page URL which can be used later to return the user to the page they were requesting when the login redirect happened. Using this method uses Response.Redirect()
which goes against the MVC mentality. You'll lose out on events like OnActionExecuting
from firing in your controller/filters. source code of RedirectToLoginPage
RedirectToAction("Login", "Account");
doesn't have the ReturnUrl
feature out of the box, but it does keep everything in the MVC ecosystem so the events fire. In the case if logging someone out and redirecting back to the login page, I'd probably keep it all in MVC and use RedirectToAction
. Or potentially use Redirect(FormsAuthentication.Url);
if you always want to use the login page URL from the web.config.
这篇关于FormsAuthentication.RedirectToLoginPage()VS RedirectToAction(QUOT;登录","帐户])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!