你不得不原谅我不知道OpenId背后的魔力。在我的流程中;用户从外部站点定向并使用OpenId进行身份验证。它们将返回到我的应用程序以创建其他用户名和密码。他们在我的应用程序中注册了他们的帐户并登录。
当他们在注册后从他们的网站启动我的应用程序(外部网站)时,我需要让用户自动登录,以兑现OpenId凭据。
我正在使用以下代码:
internal class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUri)
: this(provider, redirectUri, null) { }
public ChallengeResult(string provider, string redirectUri, string userId)
{
LoginProvider = provider;
RedirectUri = redirectUri;
UserId = userId;
}
public string LoginProvider { get; set; }
public string RedirectUri { get; set; }
public string UserId { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XSRF_KEY] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
然后重定向到此功能:
var openid = new OpenIdRelyingParty();
var response = openid.GetResponse();
if (response == null)
{
Identifier id;
if (Identifier.TryParse(_identifer, out id))
{
try
{
return openid.CreateRequest(_identifer).RedirectingResponse.AsActionResultMvc5();
} catch (ProtocolException e)
{
throw new Exception(e.Message);
}
}
return View("Login");
}
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false);
// Now I need to log in the user
break;
case AuthenticationStatus.Canceled:
break;
case AuthenticationStatus.Failed:
break;
}
从OpenIdRelyingParty返回并验证响应后,我需要登录用户。我是否需要启用表单身份验证才能使其正常工作?我正在使用MVC Identity 2.0。
答案 0 :(得分:0)
这是我在public ActionResult ExternalLoginCallback(string returnUrl)
方法中的一部分,这是从外部OpenId服务器调用的方法(在这种情况下是Intuit,但它不应该重要)。< / p>
//Attempt to log the user in if they've already created an account with the external login.
if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
{
return RedirectToLocal(returnUrl);
}