我正在尝试创建 DotNetOpenAuth OpenID MVC 3 项目,但无法接收用户身份验证状态。我的 post 方法工作正常,但是当请求返回到 HttpGet 方法时,我检查了 User.Identity.IsAuthenticated 状态,当我成功登录到任何 openID 提供程序时它返回 false。
我已经在 webconfig dotNetOpenAuth untrustedWebRequest 中启用了与 localhost 的通信,但这并没有解决问题,我花了几个小时来寻找为什么用户没有作为经过身份验证的用户返回的答案。
一定有一些逻辑错误会导致用户无法从我的代码中经过身份验证的 openID 提供程序返回,但我找不到它。
感谢您对我的问题的任何答复!
我的控制器:
namespace DotNetOpenAuth_OpenID.Controllers
{
public class UserController : Controller
{
private static OpenIdRelyingParty openid = new OpenIdRelyingParty();
public IFormsAuthenticationService FormsService { get; set; }
protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null)
{
FormsService = new AuthenticationService();
}
base.Initialize(requestContext);
}
// **************************************
// URL: /User/LogIn
// **************************************
[HttpGet]
public ActionResult LogIn()
{
if (User.Identity.IsAuthenticated) <===== RETURNS FALSE
{
return RedirectToAction("Profile", "User");
}
Identifier id;
if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
{
try
{
var req = openid.CreateRequest(Request.Form["openid_identifier"]);
return req.RedirectingResponse.AsActionResult();
}
catch (ProtocolException ex)
{
//display error by showing original LogOn view
//this.ErrorDisplay.ShowError("Unable to authenticate: " + ex.Message);
return View("Login");
}
//return LogIn(new User { OpenID = id }, Request.Form["ReturnUrl"]);
}
return View("Login");
}
[HttpPost]
public ActionResult LogIn(User model, string returnUrl)
{
string openID = ModelState.IsValid ? model.OpenID : Request.Form["openid_identifier"];
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Profile", "User");
}
else if (!string.IsNullOrEmpty(openID))
{
return Authenticate(openID, returnUrl);
}
else if (ModelState.IsValid)
{
ModelState.AddModelError("error", "The OpenID field is required.");
}
// If we got this far, something failed, redisplay form
return View(model);
}