If manager.FindByID()
returns null, you will get that exception when you try and reference properties in the currentUser
object (which of course would also be null). In addition, currentUser.MyUserInfo
could be null, causing the same issue.
Object reference not set to an instance of an object when accessing user profile information
문제
I have created a different identity table MyUserInfo for my user profile where I store additional info such as Name, address etc. The idea is that if the user has not entered his / her data then he will be sent to another page where it can be done. The issue is that when I try to verify if the client has entered his name, address etc... i get an an error and I don't understand why:
Additional information: Object reference not set to an instance of an object.
this happens at the line where I try to check if the value of the User Name is null
if (currentUser.MyUserInfo.FirstName == null)
Here is the code:
string user = System.Web.HttpContext.Current.User.Identity.Name;
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
store.AutoSaveChanges = false;
var currentUserId = User.Identity.GetUserId();
var manager = new UserManager<ApplicationUser>(store);
var currentUser = manager.FindById(User.Identity.GetUserId());
//check if the user is registered and what his role is
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated && System.Web.HttpContext.Current.User.IsInRole("Customer"))
{
if (currentUser.MyUserInfo.FirstName == null) //offending line
{
Response.Redirect("EnterUserData.aspx");
}
}
해결책
제휴하지 않습니다 StackOverflow