C# Login code not work on safari

拜拜、爱过 提交于 2020-01-05 05:32:18

问题


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");
} 

回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/41259996/c-sharp-login-code-not-work-on-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!