Redirect to custom page with FormsAuthentication using C# in ASP.Net

Last Reply on Sep 10, 2017 11:21 PM By dharmendr

Posted on Sep 06, 2017 07:34 AM

http://localhost:3834/Default.aspx?ReturnUrl=%2fState%2fGabbPage.aspx

it refused to redirect after login i dont know if its from my web.config file

 

<compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
    <authorization>
      <deny users="?" />
    </authorization>
    <authentication mode="Forms">
      <forms defaultUrl="Categories.aspx" loginUrl="Default.aspx" slidingExpiration="true" />
    </authentication>
  <pages>
      <controls>
        <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
      </controls>
    </pages></system.web>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
</configuration>

 

Posted on Sep 07, 2017 06:39 AM

As you are using form authentication and want to redirect to custom page so you need to do it like below.

micah says:
userName = dt.Rows[0]["UserName"].ToString();
                    location = dt.Rows[0]["Location"].ToString();
 
                    if (!string.IsNullOrEmpty(location) && location != "-1" && location != "-2")
                    {
                        Session["userName"] = userName;
                        switch (location)
                        {
                            case "state1":
                                Response.Redirect("~/state1/" + location + "Page.aspx");
                                break;
                            case "state2":
                                Response.Redirect("~/state2/" + location + "Page.aspx");
                                break;
                            case "state3":
                                Response.Redirect("~/state3/" + location + "Page.aspx");
                                break;
                        }
                    }
                    else
                    {
                        switch (location)
                        {
                            case "-1":
                                LoginINNOVATION.FailureText = "Username or password not correct.";
                                break;
                            case "-2":
                                LoginINNOVATION.FailureText = "Account has not been activated.";
                                break;
                        }
                    }

Replace the above code with the below.

userName = dt.Rows[0]["UserName"].ToString();
location = dt.Rows[0]["Location"].ToString();

if (!string.IsNullOrEmpty(location) && location != "-1" && location != "-2")
{
    Session["userName"] = userName;
    switch (location)
    {
        case "Texas":
            // Add this line of code.
            FormsAuthentication.SetAuthCookie(LoginINNOVATION.UserName, LoginINNOVATION.RememberMeSet);
            Response.Redirect("~/Texas/" + location + "Page.aspx");
            break;
        case "Washinton":
            // Add this line of code.
            FormsAuthentication.SetAuthCookie(LoginINNOVATION.UserName, LoginINNOVATION.RememberMeSet);
            Response.Redirect("~/Washinton/" + location + "Page.aspx");
            break;
        case "Maryland":
            // Add this line of code.
            FormsAuthentication.SetAuthCookie(LoginINNOVATION.UserName, LoginINNOVATION.RememberMeSet);
            Response.Redirect("~/Maryland/" + location + "Page.aspx");
            break;
    }
}
else
{
    switch (location)
    {
        case "-1":
            LoginINNOVATION.FailureText = "Username or password not correct.";
            break;
        case "-2":
            LoginINNOVATION.FailureText = "Account has not been activated.";
            break;
    }
}