I'm developing a web project which contains the authorisation system which uses cookies. I'm using Identity 2.0 Sample Project so I got that feature built-in. I also use Git as my version control system.
I have merged two branches. Both of them work with authorisation and cookies easily. When you log in and then stop the application via Visual Studio, I can start the application again and I will remain logged in.
After I have merged two branches I got some strange behavior. The code I have merged doesn't affect the authorisation system.
When you log in the cookie file is created, so when I manually stop the application via Visual Studio and then start it again, I'm supposed to be logged in. Howether the application crashes with the following exception:
An exception of type 'System.NullReferenceException' occurred in Microsoft.Owin.Host.SystemWeb.dll but was not handled in user code
And the Visual Studio points at the following line:
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
The code was included in Identity Sample and it had been working fine before the merge. As I have found out, if I delete following piece of code, the app works fine; I can stop it and run it again having zero exceptions:
@{
var manager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var currentUser = manager.FindById(User.Identity.GetUserId());
}
@Html.ActionLink("Hello " + currentUser.UserNickName + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
This code is a part of _LoggingPartial View which was genereated by Visual Studio and modified in branch I merged with.
So now I'm totally lost because I have no idea what that exception was caused by.
Perhaps someone could help me, give an advice on how I can solve it.
UPDATE:
As I've figrued out, the problem isn't with the nickname. Actually, the app crashes after stop-start even if I add the following code to the old vesrion:
@{
var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
var currentUser = userManager.FindById(User.Identity.GetUserId());
}
@Html.ActionLink("Hello " + currentUser.Email.ToString() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
As you see, this code doesn't use any methods but native Identity's.
Also, I've tested different methods to get the user and got the following results:
The app crashes if I use ' var currentUser = userManager.FindById(User.Identity.GetUserId());'
The app crashes if I use ' var currentUser = userManager.FindByIdAsync(User.Identity.GetUserId());' (Of course, I've modified the rest of the code to work with async result)
The app crashes if I use ' var currentUser = userManager.FindByName(User.Identity.GetUserName());'
The app does not crash if I use ' var currentUser = userManager.FindByNameAsync(User.Identity.GetUserName());'
So, neither does it work with FindById nor with FindByIdAsync. If I use FindByName, it works with FindByNameAsync but crashes with simple FindByName