2012-12-19 44 views
1

步骤使用MVC4互联网模板项目复制我的问题:MVC4外部登录交换机占

  1. 注册本地会员帐户(帐户A)
  2. 登出
  3. 注册一个OpenID帐户(IM使用谷歌)(账户B)
  4. 登出
  5. 注册回账户A
  6. 导航到帐户/管理
  7. 在对谷歌

会发生什么情况的外部链接部分,点击是账户A被注销和B账户中签署的。我希望对一些神奇连接帐户A到帐户B,或也许是一个例外。所以看起来我需要做那部分。继承我的代码到目前为止。它取代了Account Controller中的ExternalLoginCallback。

[AllowAnonymous] 
    public ActionResult ExternalLoginCallback(string returnUrl) 
    { 
     AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl })); 
     if (!result.IsSuccessful) 
     { 
      return RedirectToAction("ExternalLoginFailure"); 
     } 

     // Need to do fancy logic in case of multiple accounts 

     if (User.Identity.IsAuthenticated) 
     { 
      // check for second blocking account 
      // NOTE : This is not a real method, need a real solution 
      var second = OAuthWebSecurity.GetUserIdFromProviderUserId(result.ProviderUserId); 

      if (second != WebSecurity.CurrentUserId) 
      { 
       // redirect to failure 
       // "This Login is used by another account... " 
      } 

正如您所看到的,我坚持使用身份验证结果查找帐户。有什么方法可以使用提供者用户标识查找帐户?任何帮助或见解都会很好。


hackish的修复 伊夫想出了一个快速和肮脏的修复。

public ActionResult ExternalLoginCallback(string returnUrl) 
    { 
     AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl })); 
     if (!result.IsSuccessful) 
     { 
      return RedirectToAction("ExternalLoginFailure"); 
     } 


     // Need to do fancy logic in case of multiple accounts 
     bool isBindingAction = User.Identity.IsAuthenticated; 

     if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false)) 
     { 
      if (isBindingAction) 
      { 
       // tell the user that he has 2 accounts 
       return RedirectToAction("ExternalLoginSwitch", new {provider = result.Provider}); 
      } 
      return RedirectToLocal(returnUrl); 
     } 

回答

0

我觉得如果你刚刚更换登录的呼叫与到呼叫:

public static string GetUserName(string providerName, string providerUserId); 

,并检查用户名存在此提供商/ providerUserId,它会更好。调用登录唯一的问题是我相信会将表单验证票据设置为错误的用户。