使用MVC4 Internet模板项目复制问题的步骤:
帐户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... "
}
如您所见,我坚持使用身份验证结果查找帐户。有没有办法使用提供者用户ID查找帐户?任何帮助或见解都会很好。
Hackish Fix 我想出了一个快速而肮脏的解决方案。
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 :(得分:0)
我想如果您刚刚通过调用:
替换了Login调用public static string GetUserName(string providerName, string providerUserId);
并检查此提供者/ providerUserId是否存在用户名,这样会更好。调用Login的唯一问题是我相信会将表单auth票证设置为错误的用户。