UserManager Keeps throwing a System.ArgumentNullException
Asked Answered
F

3

9

I am trying to implemented the standard Id in AspNetUsers from nvarchar to int. I've manage to get that side working. However my issue is when I try to login I keep getting an error from the UserManager class.

My code is below:

public class UserManager : UserManager<ApplicationUser, int>
{
    public UserManager(IUserStore<ApplicationUser, int> store) 
    : base(store) 
{ 
}

And on the login page I've got

if (IsValid)
        {
            // Validate the user password
            var manager = Context.GetOwinContext().GetUserManager<UserManager>();
            var user = manager.Find(UserName.Text, Password.Text); //This line throws the error
            if (user != null)
            {
                IdentityHelper.SignIn(manager, user, isPersistent: false);
                Response.Redirect("~/Home.aspx");                }
            else
            {
                FailureText.Text = "Invalid username or password.";
                ErrorMessage.Visible = true;
            }
        }

The error I keep getting is System.ArgumentNullException: Value cannot be null. Parameter name: manager. Has anyone else come across this issue? Thanks in advance for your help

Stack Trace

[ArgumentNullException: Value cannot be null.
Parameter name: manager]
Microsoft.AspNet.Identity.UserManagerExtensions.Find(UserManager`2 manager, String userName, String password) +221
Account_Login.LogIn(Object sender, EventArgs e) in Login.aspx.cs:17
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628026
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.<ProcessRequestMainAsync>d__14.MoveNext() +5226
Farron answered 21/11, 2014 at 11:46 Comment(4)
Could you specify which line is throwing the exception?Dolabriform
@MotiAzu for me it's this line var user = manager.Find(UserName.Text, Password.Text);Farron
@Farron paste the full stack trace of your ArgumentNullExceptionSaintjust
Possible duplicate of MVC5 (VS2012) Identity CreateIdentityAsync - Value cannot be nullIchthyornis
D
15

Make sure that UserManager is registered in you OWIN context. You should have something like this in your Startup class:

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

Also ensure that you have [assembly: OwinStartup(typeof(YourNamespace.Startup))] attribute applied to your web assembly.

Doggo answered 16/12, 2014 at 12:11 Comment(5)
After trying your suggestion, I get the same error but on public CustomRoleStore(ApplicationDbContext context) line :/Farron
@Farron which means the solution is correct and you're just encountering some other problem, which might be hard to help you with, as you haven't provided any code surrounding that line.Hitherward
@Farron - I'd use your best judgment to either provide that code, or ask a separate question if it's not related (it doesn't seem to be at first glance - the bit you've provided doesn't mention your manager instance).Hitherward
@Farron maybe your ApplicationDbContext is not registered in OWIN too? Check the same thing I suggested in the answer for ApplicationDbContext as well.Doggo
@Sergey Just checked my ApplicationDbContext is registeredFarron
P
2

the manager.Find() method only search in primary key of table, you must use something like :

Context db = new Context();
if (db.Users.Select(u => u).Where(u => u.Username == Username && u.Pass == Password).ToList().Count() == 1)
{
      // login
}
Pottage answered 20/12, 2014 at 2:31 Comment(0)
F
0

I think the problem comes from the very basic of setups. Can you check that you're user manager is registered in your OWIN context. And In any case you've extended the Identity ApplicationUser attributes, please do ensure that you've made them public and that they have getters and setters.

Forestation answered 29/9, 2022 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.