gpt4 book ai didi

authentication - 使用 ExternalLoginCallback 进行身份验证如何获取 UserId 和 UserName ASP.NET MVC5

转载 作者:行者123 更新时间:2023-12-01 13:46:59 24 4
gpt4 key购买 nike

我正在使用以下操作在 ASP.NET MVC5 中进行非常基本的登录:

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
string mid = ExtractMid(returnUrl);

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
if (mid == null)
{
return RedirectToAction("Login");
}
else
{
return RedirectToAction("MobileLoginFailure");
}
}

// Sign in the user with this external login provider if the user already has a login
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
switch (result)
{
case SignInStatus.Success:
if (mid != null)
{
return redirectMobile(mid, User.Identity.GetUserName(), User.Identity.GetUserId<int>());
}
else
{
return RedirectToLocal(returnUrl);
}
case SignInStatus.Failure:
default:
return View("ExternalLoginConfirmation");
}
}

我希望在 SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false); 之后用户将通过以下方式登录:redirectMobile(mid, User.Identity.GetUserName(), User.Identity.GetUserId<int>());我将能够获取用户 ID 和用户名。但这种情况并非如此! User.Identity.GetUserName()返回 nullUser.Identity.GetUserId<int>()返回 0 .

我做错了什么?

最佳答案

我不能说我知道发生了什么。不过,我确实找到了一些满足我需求的解决方案。

我添加了这个:

var user = await UserManager.FindByEmailAsync(loginInfo.Email);

并使用了我需要的信息。为了完整性 -

        case SignInStatus.Success:
if (mid != null)
{
var user = await UserManager.FindByEmailAsync(loginInfo.Email);
if (user == null)
{
return RedirectToAction("MobileLoginFailure");
}
else
{
return redirectMobile(mid, user.UserName, user.Id);
}
}
else
{
return RedirectToLocal(returnUrl);
}

关于authentication - 使用 ExternalLoginCallback 进行身份验证如何获取 UserId 和 UserName ASP.NET MVC5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35252560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com