jom*_*mni 5 c# cookies azure razor acs
在我的网站上,有一个注册表.填写完成后,用户将被重定向到Azure ACS以便登录.登录后,用户将被重定向回我的网站并进行注册和登录.
注册表由JavaScript提交.用户填写的信息通过RegisterController中的RedirectToProvider方法保存到cookie中,用户被重定向到ACS.当用户从ACS重定向回网站时,然后通过RegisterController中的RegisterUser方法读取cookie.问题是:这在95%的时间都有效.5%的时间,当用户回来时,cookie为空.我一直无法追踪原因,我想知道是否有任何已知的问题或我可能会监督的事情.表单代码如下所示:
@using (Html.BeginForm("RedirectToProvider", "Register", FormMethod.Post, new { id = "registerForm" }))
... various fields...
<input type="button" class="btn" id="registerSubmitButton" value="Register" onclick="RegisterRedirect()" />
}
Run Code Online (Sandbox Code Playgroud)
提交表单的RegisterRedirect()JavaScript(此处省略了相关的功能):
var RegisterRedirect = function () {
$("#registerForm").valid();
$("#registerForm").submit();
}
Run Code Online (Sandbox Code Playgroud)
RegisterController中的RedirectToProvider方法:
[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." });
}
Run Code Online (Sandbox Code Playgroud)
用户被重定向到ACS并选择使用例如Gmail登录.ACS回调我的ClaimsAuthenticationManager(在web.config中配置).然后,调用要回调的方法(在ACS中配置),然后调用应该读取cookie的RegisterUser方法:
[Authorize]
public ActionResult RegisterUser(User user){
var cookie = ControllerContext.HttpContext.Request.Cookies["RegisterViewModel"];
if (cookie != null){
... registers the user...
}
}
Run Code Online (Sandbox Code Playgroud)
95%的情况下,cookie不为空.5%的时间,某些内容失败,cookie为空.Azure模拟器刚刚启动后,在网站的第一次构建期间失败率较高,之后降低.我读过它可能与会话有关.有没有人看到明显的错误或有任何建议?在此先感谢您的帮助!