locked
NullReferenceException in UserManager.Find() RRS feed

  • Question

  • User1133159508 posted

    I'm getting a mysterious NullReferenceException whenever I call UserManger.Find().

    Stack trace:

       at Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter`1.get_IsCompleted()
       at Microsoft.AspNet.Identity.UserManager`2.<FindAsync>d__12.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Microsoft.AspNet.Identity.AsyncHelper.RunSync[TResult](Func`1 func)
       at Microsoft.AspNet.Identity.UserManagerExtensions.Find[TUser,TKey](UserManager`2 manager, String userName, String password)
       at HackersDiet3.Login.btnLogin_Click(Object sender, EventArgs e) in c:\Users\bfarris\Documents\Visual Studio 2013\Projects\HackersDiet3\HackersDiet3\Login.aspx.cs:line 28
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Here's my UserStore.FindByNameAsync() method, which I can see is successfully finding and returning the user:

     public System.Threading.Tasks.Task<ApplicationUser> FindByNameAsync(string userName)
            {
                var q = new HackersDiet3.DirectQuery("select id, username, password from users where username = @username", System.Data.CommandType.Text);
                q.Parameters.Add(new HackersDiet3.DirectQueryParameter("@username", userName, System.Data.DbType.String));
    
                using (var r = q.ExecuteReader())
                {
                    if (r.Reader.Read() == false)
                        return System.Threading.Tasks.Task.FromResult<ApplicationUser>(null);
    
                    ApplicationUser user = new ApplicationUser();
                    user.Id = r.Reader.GetGuid(0).ToString();
                    user.UserName = r.Reader.GetString(1);
                    user.PasswordHash = new byte[49];
                    r.Reader.GetBytes(2, 0, user.PasswordHash, 0, 49);
    
                    return System.Threading.Tasks.Task.FromResult<ApplicationUser>(user);
                }
            }

    Am I doing something obviously wrong?

    Friday, February 20, 2015 11:24 AM

Answers

  • User1133159508 posted

    I found the problem. I was overriding UserManager.CheckPasswordAsync() and returning null rather than a Task<bool> instance.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2015 2:36 PM

All replies

  • User281315223 posted

    I don't see any references in your existing code to the "UserManager.Find()" method. Where is that currently defined?

    Since you are encountering a null reference exception, this generally means that you are attempting to reference a property, field or method of a null object. This is why it's important to always explicitly check if object is null after receiving it from a method (which could possibly return null).

    Could you post your additional code where you are calling the UserManager.Find() method?

    Friday, February 20, 2015 2:18 PM
  • User1133159508 posted

    I found the problem. I was overriding UserManager.CheckPasswordAsync() and returning null rather than a Task<bool> instance.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 20, 2015 2:36 PM