Home External ASP.NET MVC 5 Identity login throws System.ArgumentNullException
Reply: 1

External ASP.NET MVC 5 Identity login throws System.ArgumentNullException

Arthur Bardakov
1#
Arthur Bardakov Published in 2018-02-13 18:48:29Z

I use external authentication in my ASP.NET MVC 5 Identity web application. The authentication is implemented using Google account. The registration process goes just fine using Google account, but when I try to log-in (already been registered previously) an exception occurs in this part of code: var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false); The whole code snippet goes just like this:

[HttpGet]
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    if (loginInfo == null)
    {
        return RedirectToAction("Login");
    }

    try//just to catch an exception in the next line
    {
        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 });
        }

    }
    catch (Exception e)
    {
        return View();
    }
}

The exception is this:

Message: The value can not be undefined!

ParamName: "value"

Source: mscorlib

..though I don't have any "value" properties in 'loginInfo' variable to be undefined!

Arthur Bardakov
2#
Arthur Bardakov Reply to 2018-02-14 16:14:22Z

I found a misktake. The thing was that when you register using third-party services(as in my case Google), you have to make sure that all the fields that marked as [Required] has to be filled directly in the method!

Just like this:

var name = info.ExternalIdentity.Name.Split(' ');
user.LastName = name[1];
user.FirstName = name[0];

In such a method:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl, bool registerAsDoctor)
    {
        if (User.Identity.IsAuthenticated)
        {
            return RedirectToAction("MyProfile", "Home");
        }

        if (ModelState.IsValid)
        {
            var info = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return View("ExternalLoginFailure");
            }

            var user = (registerAsDoctor) ? (ApplicationUser)
                          new Doctor(model.Email) : new Patient(model.Email);

            var result = await UserManager.CreateAsync(user);
            if (result.Succeeded)
            {
                var name = info.ExternalIdentity.Name.Split(' ');
                user.LastName = name[1];
                user.FirstName = name[0];

                result = await UserManager.AddLoginAsync(user.Id, info.Login);
                if (result.Succeeded)
                {
                    await UserManager.AddToRoleAsync(user.Id, (registerAsDoctor) ? "Doctor" : "Patient");
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    RedirectToAction("MyProfile", "Home");
                }
            }
            AddErrors(result);
        }

        ViewBag.ReturnUrl = returnUrl;
        return RedirectToAction("Index", "Home");
    }

Because if one misses it, the registration will pass just fine, but on the next log-in, it will throw an exception, that some "value" can not be null!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.39997 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO