【发布时间】:2011-03-18 17:12:37
【问题描述】:
当我调用我的身份验证时,我从查询字符串传递返回 URL。当 Open Id 提供程序重定向回相同的操作结果时,Return Url 参数为 null。在整个通话过程中保持这种状态的最佳方式是什么?
人们是否在会话中存储本地返回 URL?以下是有问题的方法。
[ValidateInput(false)]
public ActionResult Authenticate(string returnUrl)
{
openId = new OpenIdRelyingParty();
IAuthenticationResponse response = openId.GetResponse();
if (response == null)
{
Identifier id;
if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
{
try
{
// at this point we have a return Url
return openId.CreateRequest(id).RedirectingResponse.AsActionResult();
}
catch (ProtocolException pex)
{
ModelState.AddModelError("", pex.Message);
return View("LogOn");
}
}
else
{
ModelState.AddModelError("", "Invalid Identifier");
return View("LogOn");
}
}
else
{
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, true);
// at this point return URL is null
var fetch = response.GetExtension<FetchResponse>();
string email = string.Empty;
if (fetch != null)
email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
if (!string.IsNullOrEmpty(returnUrl))
{
var test = FormsAuthentication.GetRedirectUrl(User.Identity.Name, false);
var url = AppHelper.GenerateReturnURL(Request, returnUrl);
return Redirect(url);
}
else
{
return RedirectToAction("Index", "Home");
}
case AuthenticationStatus.Canceled:
ModelState.AddModelError("", "Canceled at provider");
return View("LogOn");
case AuthenticationStatus.Failed:
ModelState.AddModelError("", response.Exception.Message);
return View("LogOn");
}
}
return View("LogOn");
}
【问题讨论】:
-
您确定在您的表单最初发布到您的 Authenticate 操作之前填充了 returnUrl 值吗?在调用 OpenID 提供程序之前?
-
积极。正如我对查询字符串所期望的那样,它充满了 /Admin。当它返回到相同的操作时,它是空的。您是否希望它在 OpenID 提供程序调用之后仍然存在?
-
returnUrl 是作为 URL 查询字符串而不是字段帖子提交的吗?例如
标签: asp.net-mvc dotnetopenauth