1

I used the code below to login to my asp.net website. with framework 4 it's work successfully at web bowers, but didn't work on safari (iphone, ipad) only when I click on login button he refresh the page and not login.

Session["AdminID"] = DT.Rows[0]["Id"].ToString();
Response.Cookies.Add(new HttpCookie("SuperAccountId", DT.Rows[0]["Id"].ToString()));
Response.Cookies["SuperAccountId"].Expires = System.DateTime.Now.AddDays(1);

Response.Cookies.Add(new HttpCookie("SuperAccountName", DT.Rows[0]["Username"].ToString()));
Response.Cookies["SuperAccountName"].Expires = System.DateTime.Now.AddDays(1);
FormsAuthentication.SetAuthCookie(Session["AdminID"].ToString(), true);
FormsAuthentication.RedirectFromLoginPage("admin", true);

//create a cookie
HttpCookie myCookie = new HttpCookie("FirstLoginCookies");

//Add key-values in the cookie
myCookie.Values.Add("first", "1");

//set cookie expiry date-time. Made it to last for next 12 hours.
myCookie.Expires = DateTime.Now.AddHours(12);

//Most important, write the cookie to client.
Response.Cookies.Add(myCookie);

if (Request.QueryString["ReturnUrl"] != null)
{
     string redirectURL = Request.QueryString["ReturnUrl"].ToString();
     Response.Redirect("~" + redirectURL);
}
else
{
     Response.Redirect("~/admin");
} 
Keith
  • 150,284
  • 78
  • 298
  • 434
user1851825
  • 31
  • 10
  • i guess by default cookies are disabled on safari browsers – Sameer Dec 21 '16 at 10:03
  • I'm try to make it allow for all website, but the problem still the same – user1851825 Dec 21 '16 at 10:09
  • first you need to check are cookies enabled than only try to set cookies.. or else use a different approach..try to use browsers local stroage . most of modern browsers support it – Sameer Dec 21 '16 at 10:10

2 Answers2

1

Make sure you have no error in your master page. (If you are using a master page) Then try editing the last part of your code like below:

                if (Request.QueryString["ReturnUrl"] != null)
                {
                    string redirectURL = Request.QueryString["ReturnUrl"].ToString();
                    Response.Redirect("~" + redirectURL, false);

                }
                else
                {
                    Response.Redirect("~/admin", false);
                } 

By setting the second parameter of Response.Redirect to "false" the original page won't be posted back to the browser and you should be redirected to the new page.

Benjamin
  • 3,499
  • 8
  • 44
  • 77
0

First check that the cookie is set in the response headers.

Then check that the cookie is included in the next request made by the browser.

Then check the details of the cookie - the authentication should be HTTP only and secure only. Check the path of the cookie as these appear to be case insensitive in .NET and in page URLs, but they're actually case sensitive. The configuration for the cookie set by FormsAuthentication.SetAuthCookie is in your web.config.

Try using fewer cookies, especially on mobile - they're included with every future request so add a kind of bandwidth tax to every page. That's worth it for the authentication cookie, but the rest are better off stored in server side sessions or client side (in local storage or indexed DB, both of which mobile Safari supports) directly.

Keith
  • 150,284
  • 78
  • 298
  • 434