1
步骤使用MVC4互联网模板项目复制我的问题:MVC4外部登录交换机占
- 注册本地会员帐户(帐户A)
- 登出
- 注册一个OpenID帐户(IM使用谷歌)(账户B)
- 登出
- 注册回账户A
- 导航到帐户/管理
- 在对谷歌
会发生什么情况的外部链接部分,点击是账户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);
}