2014-03-28 363 views
2

我有以下代碼用於驗證用戶使用谷歌進行身份驗證。我已經嘗試了3種方法來做到這一點(設置用戶屬性的2 signin()&)。 登錄似乎沒有做任何事情(用戶屬性根本沒有改變),甚至設置用戶屬性的方法,它似乎不記得的變化,我最終在一個無限的重定向循環,我不能找出原因。Google身份驗證OWIN ASP.NET

PS:路由&代碼流確實有效,它只是當我重定向到「home index」時,身份驗證無法識別,我被髮回到這個邏輯。

供參考:ChallengeResult類是生成的助手類。

public ActionResult ExternalLogin() 
    { 
     // Request a redirect to the external login provider 
     return new ChallengeResult("Google", Url.Action("ExternalLoginCallback", "GoogleAuth")); 
    } 

    [Route("ExternalLoginCallback")] 
    public async Task<ActionResult> ExternalLoginCallback() 
    { 
     var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
     var externalIdentity = await HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie); 
     var emailClaim = externalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email); 
     var email = emailClaim.Value; 
     if (loginInfo != null && (email.Contains("@mydomain.com") || email.Contains("@mydomain.ca"))) 
     { 
      //AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 
      //AuthenticationManager.SignIn(externalIdentity); 
      //AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, externalIdentity); 

      AuthenticationManager.User = new ClaimsPrincipal(externalIdentity); 

      return RedirectToAction("Index", "Home"); 
     } 
     else 
     { 
      return new HttpUnauthorizedResult(); 
     } 
    } 

    private IAuthenticationManager AuthenticationManager 
    { 
     get 
     { 
      return HttpContext.GetOwinContext().Authentication; 
     } 
    } 

在家居控制器:

[Authorize] 
    public ActionResult Index() 
    { 
     return View(); 
    } 
+0

要添加一些信息,當'返回RedirectToAction(「指數」,「家」);'被調用,它似乎認爲它仍然是未經授權的,並讓我回到了'公衆的ActionResult ExternalLogin()' – LostBalloon

+0

請檢查以下解決方案 http://stackoverflow.com/questions/23614000/intermittent-asp-net-oauth-issue-with-google-authenticationmanager-getexternali/23685160#23685160 –

回答