I'm refactoring an old website to use OWIN and claims with forms and external loigins with ASP Identity 2.
I have a question around the proper way to create a new user with external login.
I'm using the MVC scaffolding code but have a custom UserStore, UserManager and signinmanager and everything is mostly working.
The account control has in the ExternalLoginCallback method has a case with a comment to redirect to ExternalLoginConformation when a user is not found, but I am unsure where to short circuit the login logic so it wont throw an exception.
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
case SignInStatus.Failure:
default:
// If the user does not have an account, then prompt the user to create an account
ViewBag.ReturnUrl = returnUrl;
ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
}
The SignInManager.ExternalSignInAsync method flows through the userstore and usermanager and signinmanager in the following mannor. Where and how is the best way to short the logic to get the result as a failure?
AccountController : ExternalLogin(Provider, returnURL)
My Owin Middleware
AccountController : ExternalLoginCallBack AuthenticationManager returns loginInfo with all the details
UserManager : Task FindAsync(UserLoginInfo) Calls UserStore : Task FindAsync(UserLoginInfo) UserLoginInfo has Provider and Key and this is where i find there is NO user in the system. No matter what Task i return it wont stop the flow.
... UserStore : Lockout and other misc stuff - Needs a User object even if empty ...
SignInManager : Task SignInAsyn(User, Persistent, Remeber)- User object is empty
SignInManager : Task CreateUserIdentityAsync(User)- User object is empty
User : Task GenerateUserIdentityAsync(UserManager manager)
UserManager : CreateIdentityAsync(User, Auth type) user is empty and auth type = "external cookie" This throws a NULL Exception.
Solution Found. I can return a null task from the UserManager FindAsync method and it will result in a failure result.
return Task.FromResult<MyUser>(null);
Solution Found. I can return a null task from the UserManager FindAsync method and it will result in a failure result.
return Task.FromResult<MyUser>(null);