Tuesday, August 24, 2010

How to append RootUrl to ReturnUrl parameter in login page

We can append Root Url to return url parameter in the login page and do a response redirect to same page so that return url value will get updated to routed value.
Example:
public partial class Login : Page
{
    public string RootUrl
    {
        get
        {
            Uri requestUri = Context.Request.Url;
            HttpRequest request = Context.Request;
            string rootUrl = string.Format("{0}{1}{2}{3}{4}",
                requestUri.Scheme,
                Uri.SchemeDelimiter,
                requestUri.Host,
                requestUri.IsDefaultPort ? string.Empty : string.Format(":{0}", requestUri.Port),
                request.ApplicationPath);
            return rootUrl.EndsWith("/") ? rootUrl : string.Format("{0}/", rootUrl);
        }
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        string returnUrl = this.Request.QueryString.Get("ReturnUrl");
        if (!string.IsNullOrEmpty(returnUrl))
        {
            if (!returnUrl.ToLower().Contains("http://"))
            {
                returnUrl = Path.Combine(this.RootUrl, returnUrl);
                string url = string.Format("{0}?ReturnUrl={1}", this.Request.Path, Server.UrlEncode(returnUrl));
                this.Response.Redirect(url);
            }
        }
    }
}
Referene

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...