0

如果您不瞭解OpenId背後的魔法,您將不得不原諒我。在我的流程中;用戶從外部站點進行導向並使用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

這是我在我的public ActionResult ExternalLoginCallback(string returnUrl)方法中的一部分,該方法是從外部OpenId服務器調用的方法(在這種情況下爲Intuit,但不應該)。

//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); 
}