Cookie is null in rare cases after redirecting to ACS and back

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 10:49:35

问题


On my website, there is a registration form. After having filled this in, the user gets redirected to Azure ACS in order to log in. After having logged in, the user gets redirected back to my website and is to be registered and logged in.

The registration form is submitted by a JavaScript. The information that the user has filled in is saved to a cookie by the RedirectToProvider method in the RegisterController and the user is redirected to ACS. When the user has been redirected back to the website from ACS, the cookie is then read by the RegisterUser method in the RegisterController. The problem is: this works 95% of the time. 5% of the time, the cookie is null when the user comes back. I have been unable to track the cause of this and am wondering if there are any known issues or something that I may have overseen. The form code looks like this:

@using (Html.BeginForm("RedirectToProvider", "Register", FormMethod.Post, new { id = "registerForm" }))

    ... various fields...

    <input type="button" class="btn" id="registerSubmitButton" value="Register" onclick="RegisterRedirect()" />
}

The RegisterRedirect() JavaScript that submits the form (with irrelevant functionality left out here):

var RegisterRedirect = function () {
    $("#registerForm").valid();
    $("#registerForm").submit();
}

The RedirectToProvider method in the RegisterController:

[AllowAnonymous]
[HttpPost]
public ActionResult RedirectToProvider(RegisterViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        var providerUrl = viewModel.SelectedProviderUrl;
        viewModel.SelectedProviderUrl = "";

        var json = JsonConvert.SerializeObject(viewModel);

        try
        {
            var cookie = new HttpCookie("RegisterViewModel", json)
                {
                    Expires = DateTime.Now.AddMinutes(10)
                };
            ControllerContext.HttpContext.Response.Cookies.Add(cookie);
        }
        catch (FormatException)
        {
            return RedirectToAction("Index", "Error", new { reason = "Cookie saving error." });
        }
        return Redirect(providerUrl);
    }
    return RedirectToAction("Index", "Error", new { reason = "Invalid data. Try again." });
}

The user is redirected to ACS and chooses to log in with, for example, Gmail. ACS calls back to my ClaimsAuthenticationManager (configured in web.config). Afterwards, the method to be called back to (configured in ACS) is called and in turn calls the RegisterUser method that is supposed to read the cookie:

[Authorize]
public ActionResult RegisterUser(User user){
    var cookie = ControllerContext.HttpContext.Request.Cookies["RegisterViewModel"];
    if (cookie != null){
        ... registers the user...
    }
}

95% of the time, the cookie is not null. 5% of the time, something fails and the cookie is null. The fail rate is higher during the first builds of the website after the Azure Emulator has just started, and lower later on. I have read that it could have something to do with sessions. Does anyone see an obvious error or have any advice? Thanks in advance for any help!


回答1:


I think that the problem is due to the fact that you sometimes get redirected to a different web role instance where the cookie you created is missing.



来源:https://stackoverflow.com/questions/20395132/cookie-is-null-in-rare-cases-after-redirecting-to-acs-and-back

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