How to remove returnurl from url? How to remove returnurl from url? asp.net asp.net

How to remove returnurl from url?


This is the nature of Forms Authentication. (which im guessing you're using).

That is, when you access a page which requires authentication, ASP.NET will redirect you to the login page, passing in the ReturnUrl as a parameter so you can be returned to the page you came from post-login.

To remove this functionality would break the semantics and design of Forms Authentication itself. (IMO)

My suggestion - if you dont need it, dont use it.

I'm trying to redirect the user to a static page after login to do some selections.

Piece of cake - after you've done your login, instead of doing FormsAuthentication.RedirectFromLoginPage (which uses that very ReturnUrl QueryString parameter), just use FormsAuthentication.SetAuthCookie and redirect wherever you want.


Add this to your Global.asax file.

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 );  }


Create a custom Authorize Attribute

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);        }    }}

then use it on your controller

[CustomAuthorizeAttribute]public ActionResult Login(){    return View();}