I am doing a very basic Login in ASP.NET MVC5 using the following Action:
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");
}
}
I'd expect that after SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false); the user will be logged-in so that by: redirectMobile(mid, User.Identity.GetUserName(), User.Identity.GetUserId<int>()); I'll be able to get the User's Id and the user's name.
But this is not the case! User.Identity.GetUserName() returns null and User.Identity.GetUserId<int>() returns 0.
What am I doing wrong?
I can't say that I know what's going on. However, I did find some solution for my needs.
I added this:
var user = await UserManager.FindByEmailAsync(loginInfo.Email);
and used the information I needed from there. For completeness -
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With