How to add custom validation to an OWIN based authentication workflow in ASP.NET MVC 5?

228 Views Asked by At

I'm using ASP.NET Identity 2 in an ASP.NET MVC 5 project that exclusively authenticates via external provider and so far everything works. What I want to do is to add a custom validation procedure using custom user data.

For example, let's say my IUser model has a custom bool field (Active). When doing external auth, /Account/ExternalLoginCallback does the following:

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

// 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:
        return RedirectToLocal(returnUrl);
    case SignInStatus.LockedOut:
        return View("Lockout");
    case SignInStatus.RequiresVerification:
        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
    case SignInStatus.Failure:
    default:
        //...
}

The auth cookie is created, it seems, in the SignInManager.ExternalSignInAsync invocation.

What I want to know is if there's any way I can hook this call to SignInManager.ExternalSignInAsync and append my own validation process (checking if the user's Active flag is set true, for example) so I can make the method return SigInStatus.Failure in case my custom process fails (following the example, Active is false).

Thanks in advance.

0

There are 0 best solutions below