How to remove returnurl from url?

Viewed 67678

I want to remove "returnurl=/blabla" from address bar when a user want to access to a login required page. Because I'm trying to redirect the user to a static page after login to do some selections.

How can I do that?

11 Answers

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

If you want to remove returnURL from request and redirect to specific path, you can follow this steps.

Firstly get the current context, verify if the user is authenticated and finally redirect the current path.

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

        }

I put this code into Page_Load method from my class Login.aspx.cs

In the loginUrl of the forms authentication configuration element use a proxy target such as "relogin.aspx".

When this "relogin.aspx" page is loaded, redirect to "login.aspx" without supplying the ReturnUrl in the query string.

Now the "login.aspx" page won’t have the ReturnUrl in the address. ta-da!

If the return URL value is required for a post-login action you’ll need to find another way to pass it to the real login page.

Use FormsAuthentication.SetAuthCookie after authentication (instead of RedirectFromLoginPage which would use the returnUrl configuration) and explicitly redirect as relevant.

Related