【发布时间】:2011-04-12 13:59:48
【问题描述】:
当用户想要访问需要登录的页面时,我想从地址栏中删除“returnurl=/blabla”。因为我试图在登录后将用户重定向到静态页面以进行一些选择。
我该怎么做?
【问题讨论】:
标签: asp.net asp.net-mvc forms-authentication
当用户想要访问需要登录的页面时,我想从地址栏中删除“returnurl=/blabla”。因为我试图在登录后将用户重定向到静态页面以进行一些选择。
我该怎么做?
【问题讨论】:
标签: asp.net asp.net-mvc forms-authentication
这是表单身份验证的本质。 (我猜你正在使用)。
也就是说,当您访问需要身份验证的页面时,ASP.NET 会将您重定向到登录页面,并将 ReturnUrl 作为参数传入,以便您可以返回到登录后的页面。
删除此功能会破坏表单身份验证本身的语义和设计。 (海事组织)
我的建议 - 如果你不需要它,不要使用它。
我正在尝试将用户重定向到 登录后做一些静态页面 选择。
小菜一碟 - 完成登录后,不要执行 FormsAuthentication.RedirectFromLoginPage(它使用 ReturnUrl QueryString 参数),只需使用 FormsAuthentication.SetAuthCookie并重定向到任何你想要的地方。
【讨论】:
将此添加到您的 Global.asax 文件中。
public class MvcApplication : HttpApplication {
private const String ReturnUrlRegexPattern = @"\?ReturnUrl=.*$";
public MvcApplication() {
PreSendRequestHeaders += MvcApplicationOnPreSendRequestHeaders;
}
private void MvcApplicationOnPreSendRequestHeaders( object sender, EventArgs e ) {
String redirectUrl = Response.RedirectLocation;
if ( String.IsNullOrEmpty(redirectUrl)
|| !Regex.IsMatch( redirectUrl, ReturnUrlRegexPattern ) ) {
return;
}
Response.RedirectLocation = Regex.Replace( redirectUrl,
ReturnUrlRegexPattern,
String.Empty );
}
【讨论】:
Too many Redirects。我认为问题是,当控件像mywebsite.com/Login 一样转到Login 页面时,它会检查身份验证并重定向到Login.aspx 页面。并且您的代码再次重定向到 Login page 。这个循环继续。你能帮我吗???
:base() 吗?
创建自定义授权属性
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(
AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
string loginUrl = "/"; // Default Login Url
filterContext.Result = new RedirectResult(loginUrl);
}
}
}
然后在你的控制器上使用它
[CustomAuthorizeAttribute]
public ActionResult Login()
{
return View();
}
【讨论】:
HandleUnauthorizedRequest 中并使用这一行代替filterContext.Result = new RedirectResult( FormsAuthentication.LoginUrl ); 更有意义
简单...
[AllowAnonymous]
public ActionResult Login() { return View(); }
[AllowAnonymous]
public ActionResult LoginRedirect(){ return RedirectToAction("Login"); }
网络配置
<authentication mode="Forms">
<forms loginUrl="~/Account/LoginRedirect" timeout="2880" />
</authentication>
【讨论】:
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(("/Account/LoginRedirect")), ... 我还使用了 RedirectToActionPermanent 而不是 RedirectToAction,因为它是永久的。
正如 RPM1984 指出的那样,您不必在登录后将用户重定向到指定的 URL。
如果您必须删除ReturnUrl 查询字符串参数,则有几个选项。可能最简单的方法是在您的登录网页/控制器中检查Request.QueryStrings 集合中是否存在ReturnUrl 参数。如果存在,您可以重定向回登录页面,但没有ReturnUrl。
另一种选择是为FormsAuthenticationModule 创建一个自定义实现,该类根据表单身份验证票处理用户身份验证,并负责将未经授权的用户重定向到登录页面。不幸的是,FormsAuthenticationModule 类的方法不是虚拟的,因此您无法创建派生类并覆盖所需的方法,但好消息是该类非常简单——总共可能只有 100-200 行代码,并使用 Reflector,您可以快速创建自己的自定义 FormsAuthenticationModule 类。如果你走这条路(我不推荐),你需要做的就是取出OnLeave 方法中的代码,该方法附加了ReturnUrl 参数。 (除了修改此类之外,您还需要配置 Web.config 文件,以便您的应用程序使用您的自定义 FormsAuthenticationModule 类,而不是 .NET Framework 中的类。)
编程愉快!
【讨论】:
将位置标签添加到您的web.config。如果您的页面位于子目录中,请将web.config 添加到子目录中。
<location path="ForgotPassword.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
ASP 将忽略添加 ReturnUrl 查询字符串并定向到登录。
【讨论】:
如果你使用asp.net control loginstatus 然后点击login status control press f4(for properties) 在行为部分我们可以看到LogOutAction那里选择Return to Login page。
注意:为了成功实现它,你必须有一个名为 login.aspx 的登录页面
【讨论】:
如果您想从请求中删除 returnURL 并重定向到特定路径,您可以按照以下步骤操作。
首先获取当前上下文,验证用户是否认证,最后重定向当前路径。
HttpContext context = HttpContext.Current;
//verify if the user is not authenticated
if (!context.User.Identity.IsAuthenticated)
{
//verify if the URL contains ReturnUrl
if (context.Request.Url.ToString().Contains("ReturnUrl"))
{
//redirect the current path
HttpContext.Current.Response.Redirect("~/login.aspx");
}
}
我将此代码从我的类 Login.aspx.cs 中放入 Page_Load 方法中
【讨论】:
您可以使用 HttpUtility.ParseQueryString 删除该元素。如果您使用 VB.NET,那么此代码会执行此操作
Dim nvcQuery As NameValueCollection
Dim strQuery As String = ""
If Not IsNothing(Request.QueryString("ReturnUrl")) Then
If Request.QueryString("ReturnUrl").Length Then
nvcQuery = HttpUtility.ParseQueryString(Request.QueryString.ToString)
For Each strKey As String In nvcQuery.AllKeys
If strKey <> "ReturnUrl" Then
If strQuery.Length Then strQuery += "&"
strQuery += strKey + "=" + nvcQuery(strKey)
End If
Next
If strQuery.Length Then strQuery = "?" + strQuery
If Request.CurrentExecutionFilePath <> "/default.aspx" Then
Response.Redirect(Request.CurrentExecutionFilePath + strQuery)
Else
Response.Redirect("/" + strQuery)
End If
Response.Write(Server.HtmlEncode(strQuery))
End If
End If
我会将它放在 Page.Init 事件中 - 显然您需要更改“/default.aspx”以匹配您登录页面的 URL。
【讨论】:
void Application_BeginRequest(object s, EventArgs e)
{
// ................
// strip return Return Url
if (!string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]) && Request.Path.IndexOf("login.aspx")!=-1)
System.Web.HttpContext.Current.Response.Redirect("~/login.aspx");
【讨论】:
在loginUrl of the forms authentication configuration element 中使用代理目标,例如“relogin.aspx”。
加载此“relogin.aspx”页面时,重定向到“login.aspx”而不在查询字符串中提供 ReturnUrl。
现在“login.aspx”页面的地址中不会有 ReturnUrl。 哒哒!
如果登录后操作需要返回 URL 值,您需要找到另一种方法将其传递到真实登录页面。
在身份验证后使用FormsAuthentication.SetAuthCookie(而不是使用returnUrl 配置的RedirectFromLoginPage)并在相关时显式重定向。
【讨论】: