0

I have extended the ApplicationUser to include some other information about a user:

public class ApplicationUser : IdentityUser
{
    public string AmptechEmployeeID { get; set; }
    public EnumIdentityNavigationType NavigationType { get; set; }
    public EnumPanelBarExpandMode PanelBarExpandMode { get; set; }
    public EnumIdentityTheme Theme { get; set; }
    public EnumIdentityBreadcrumbs Breadcrumbs { get; set; }
    public EnumIdentityHeaderFooter Header { get; set; }
    public EnumIdentityHeaderFooter Footer { get; set; }

Now when the user logs into the application I would like to retrieve the information and store it in cookies:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            Response.AppendCookie(GetUserInfo());
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt, the User Name and/or Password you supplied is/are invalid");
            return View(model);
    }
}

public HttpCookie GetUserInfo()
{
    var db = new ApplicationDbContext();
    var userid = User.Identity.GetUserId();
    ApplicationUser CurUser = db.Users.Find(userid);

    HttpCookie cookie = new HttpCookie("UserInfo");
    cookie["UserName"] = CurUser.UserName;
    cookie["AmptechEmployeeID"] = CurUser.AmptechEmployeeID;
    cookie["Breadcrumbs"] = CurUser.Breadcrumbs;
    cookie["Footer"] = CurUser.Footer;
    cookie["Header"] = CurUser.Header;
    cookie["NavigationType"] = CurUser.NavigationType;
    cookie["PanelBarExpandMode"] = CurUser.PanelBarExpandMode;
    cookie["Theme"] = CurUser.Theme;

    return cookie;
}

When I run the application I am getting the following error:

enter image description here

What am I doing wrong here?

Linger
  • 14,942
  • 23
  • 52
  • 79
  • Probably your `CurUser` is null. Debug the code and see which object is null that you're trying to access. – NocFenix Oct 05 '16 at 19:35
  • i think **CurUser** is null. check that **CurUser** is not null – Zulqarnain Jalil Oct 05 '16 at 20:09
  • What you are trying to do is best achieved by claims - all your data will be part of authentication cookie and easily available. Have a look on this sample: http://stackoverflow.com/a/21690079/809357 – trailmax Oct 05 '16 at 21:37

0 Answers0