ASP.NET Forms Authentication Cookie not set in Saf

2019-07-14 14:04发布

问题:

I have a ASP.NET Web Service which exposes a method called DoLogin

[WebService(Namespace = "http://rtns.ism.ec/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]    
[ScriptService]
public class UserManagerService : WebServiceBase
{

    [WebMethod(EnableSession=true)]
    [ScriptMethod]
    public ResponseBase<bool> DoLogin(LoginCredentials Credentials)
    {
        Credentials.IpAddress = HttpContext.Current.Request.UserHostAddress;
        Credentials.ServerID = Environment.MachineName;
        Credentials.SystemID = WebConfigurationManager.AppSettings["SYSTEM_ID"];
        Credentials.UserAgent = HttpContext.Current.Request.UserAgent;

        try
        {
            DataResponse<User> resp = UserManager.LoginUser(Credentials);

            if (resp.Code)
            {
                FormsAuthentication.SetAuthCookie(Credentials.Email, true);
                HttpContext.Current.Session.Add(Constants.Identifiers.USER_SESSION_INFO_IDENTIFIER, resp.Data);
            }
            return resp;
        }
        catch (Exception _ex)
        {
            ISM.Essentials.Business.LogManager.SaveLog(_ex);
            return new ResponseBase<bool> { Message = MessageManager.Instance[Constants.Messages.E_GENERIC_FAILURE, Credentials.CultureID] };
        }
    }

}

I have a JQuery client, which makes the call:

function loginSubmitHandler() {

    var objeto = {
        Email: $('#txtUser').val(),
        Password: $('#txtPassword').val(),
        CultureID: $('#hddCulture').val()
    };

    utils.startAjaxCall('../Services/UserManagerService.asmx/DoLogin', { Credentials: objeto }, function(data) {

    if (data.d.Code) {
        window.location.replace('home.aspx');
    }
    else
    {
        utils.alert(locals.Information, locals.Accept, data.d.Message);            
    }
    });

    return false;
}

When I log in with icorrect credentials, the alert with the message sent from the server appears. If I give correct credentials, the page is redirected to home.aspx

This code is working 100% fine since the begining in the following browsers:

  • IE6, 7, 8, 9
  • Mozilla
  • IE9 Windows Phone
  • Android 2.3 Browser
  • Safari 5.1 for Windows

I've just got a Mac (first ever) and when I tried to access my website, I noticed an extrange behavior. I give correct login credentials, I'm redirected to home, but the FormsAuthentication mechanism redirects back to the login page.

It seems like the Auth cookie returned back from the server is just ignored.

This is not an issue with cross domain cookies, as I'm calling the web server in the same web application/domain.

Any ideas on how to make Safari for Mac to accept the cookies returned in an Ajax Web Service call?

回答1:

The problem might be that safari will not set cookies with non-ASCII characters. Try using encodeURIComponent() function on cookie values. Here is a link with similar problem: Strange problem with cookies in Safari and Asp.net