I have a custom login system which works fine but I would like to add more fields to be retrieved from User.Identity if at all possible.
I basically have a login form which looks like this:
if (ModelState.IsValid)
{
    string Identity = model.UserName;
    string password = model.Password;
    try
    {
        var User = (from u in ctx.Users
                    where u.UserName == model.UserName
                    select u).SingleOrDefault();
        bool userValid = ctx.Users.Any(user => user.EmailAddress == Identity || user.UserName == Identity) && Crypto.VerifyHashedPassword(User.Password, password);
        //bool userValid = ctx.Users.Any(user => user.EmailAddress == Identity || user.UserName == Identity) && User.Password == password;
        if (userValid)
        {   
            FormsAuthentication.SetAuthCookie(User.Name, false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View();
        }
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View();
    }
}
return View(model);
I then retrieve the UserName by simply using User.Identity.Name
This works fine and stores the username in the Authcookie. WHat I was hoping I could do is add an ID or if possible even more details to the cookie.
Now I realise there are several ways around this.
I could create a helper which takes the username, hopes it's unique (WHich it should be but for scalability reasons I would rather not rely on this), and then uses the UserName to get the relevant fields from the database.
Another option would be to create another cookie but I don't really want to do that either. |
Lastly I thought I could concatinate all of the users details into one long string seperated by a delimiter and then split them when I retrieve the details.
The thing is, I feel like there must be a better way of doing it than the above.
So can anyone more experienced in this than me tell me how I could store more fields in the Auth Cookie? It would be greatly appreciated.
P.s. I realise I could use WebSecurity but I only discovered this after I had written my login code and I am not sure I want to change it and learn how to do WebSecurity.
Thanks in advance
 
     
     
    