3
votes

Does anyone know why ASP.NET Forms Authentication does not work on windows safari, or better yet, how to get it to work? It seems like a very weird issue. When I use a login control (System.Web.UI.WebControls.Login) everything works fine, but if I try to do a custom Forms Authentication login when I call FormsAuthentication.RedirectFromLoginPage safari just sends me back to the login page as if I'm not authenticated whereas every other browser logs me in and sends me on my way.

protected void lnkLogin_Click(object sender, EventArgs e)
{
    if (Membership.Provider.ValidateUser(txtUsername.Text, txtPassword.Text))
    {
        Session.Clear();
        HttpContext.Current.Response.Cookies.Clear();
        FormsAuthentication.SetAuthCookie(txtUsername.Text, true);
        FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, true);
    }
}
2

2 Answers

1
votes

Try either SetAuthCookie, or RedirectFromLoginPage. The redirect needs to know where to redirect to anyway (ReturnUrl), maybe that is your problem.

    if (Request.QueryString["ReturnUrl"] != null) 
    { 
        FormsAuthentication.RedirectFromLoginPage("someuserid", false); 
    } 
    else 
    { 
        FormsAuthentication.SetAuthCookie("someuserid", false); 
        Response.Redirect("~/SomePage.aspx"); 
    } 
0
votes

This works fine for me in Safari:

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        //check login
        User user = UserBAL.GetUser(Login1.UserName, Login1.Password);

        //null and filled object check
        if (user != null && user.Id > 0 && user.Roles != null && user.Roles.Count > 0)
        {

            e.Authenticated = true;

            FormsAuthenticationTicket authTicket = new
            FormsAuthenticationTicket(1,                          //version
                                     Login1.UserName,           // user   name
                                     DateTime.Now,               // creation
                                     DateTime.Now.AddMinutes(60),//  Expiration
                                     false,                      // Persistent
                                     string.Join("|", user.Roles.ToArray())); // User ata


            // Now encrypt the ticket.
            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
            // Create a cookie and add the encrypted ticket to the
            // cookie as data.
            HttpCookie authCookie =
                         new HttpCookie(FormsAuthentication.FormsCookieName,
                                        encryptedTicket);

            Response.Cookies.Add(authCookie);

            //redirect 
            Response.Redirect(FormsAuthentication.GetRedirectUrl(
                                           Login1.UserName,
                                           false));

        }
        else
        {

            Login1.FailureText = "Login failed.";
        }

    }