You must Sign In to post a response.
  • Category: Windows 10

    How do Redirect HomePagevAfter Login Successfuliy

    after successfully login, if I copy the URL and paste it into same browser tab then it should be redirect homepage page use asp .net mvc4. Here use this Code
    HomeController.cs
    public ActionResult DomainLogin(string returnUrl){}
    public string Loginss(LoginModel model, string returnUrl){}
    use Cookies for FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
    RouteConfig.cs
    public static void RegisterRoutes(RouteCollection routes)
    {
    HttpContext ctx = HttpContext.Current;
    if (ctx.Session == null)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
    );
    }
    else
    {
    if (ctx.Session != null)
    {
    if (ctx.Session.IsNewSession)
    {
    string sessionCookie = ctx.Request.Headers["Cookie"];
    //if ((sessionCookie != null) || (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
    if (sessionCookie != null)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
    name: "Home_Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    }
    }
    }

    }

    }
    }

    Global.asax.cs
    protected void Session_Start(object sender, EventArgs e)
    {
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
  • #768598
    This is one of the common restrictions you have to apply on a web application. If user is already login to the application and user knows the page extension user can directly copy and paste the URL in web browser to open the page, because user has got already an active session.

    In such cases what we have to do is, we will make use of UrlReferrer property.

    string strPreviousPage = "";
    if (Request.UrlReferrer != null)
    {
    strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
    }
    if(strPreviousPage =="")
    {
    Response.Redirect("~/Login.aspx");
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments