Adding to User Properties Using DB First asp.net webforms RRS feed

  • Question

  • User36582938 posted

    I am currnelty using form tickets to save my user session but i need to pass extra user data so i can access at code time like User.

                    UserData userData = new UserData
                        fullName = _loginUser.firstName,
                        userName = _loginUser.LoweredUserName,
                        userId = _loginUser.UserId
                    string[] roles = new string[3];
                    if (_loginUser.canAdd == true)
                        roles[0] = "canAdd";
                    if (_loginUser.canDelete == true)
                        roles[1] = "canDelete";
                    if (_loginUser.canEdit == true)
                        roles[2] = "canEdit";
                    string _roles = String.Join(",", roles);
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                      1,                                     // ticket version
                      _loginUser.UserName,                              // authenticated username
                      DateTime.Now,                          // issueDate
                      DateTime.Now.AddMinutes(30),           // expiryDate
                       chkRememberMe.Checked,                          // true to persist across browser sessions
                      _roles,                 // can be used to store additional user data
                      FormsAuthentication.FormsCookiePath);  // the path for the cookie

    Obv I am using the forms identy to read the vairables but what I want to no is how do i add my own. I want to add like player id and few other things that make up the players profile that i need be able to access at page level I am using masterpages.

    Saturday, December 5, 2015 6:49 PM

All replies

  • User614698185 posted

    Hi buckd,

    This sample shows a webform hosting the ASP.NET login control and the associated OnAuthenticate event:

    // Initialize FormsAuthentication
    // Create a new ticket used for authentication
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1, // Ticket version
        Login.UserName, // Username associated with ticket
        DateTime.Now, // Date/time issued
        DateTime.Now.AddMinutes(30), // Date/time to expire
        true, // "true" for a persistent user cookie
        "admin, privilegeduser", // User-data, in this case the roles
        FormsAuthentication.FormsCookiePath);// Path cookie valid for
    // Encrypt the cookie using the machine key for secure transport
    string hash = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, // Name of auth cookie
        hash); // Hashed ticket
    // Set the cookie's expiration time to the tickets expiration time
    //if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
    // Add the cookie to the list for outgoing response

    The user is authenticated and then an authentication cookie attached to the validating Response.

    The next step is to handle (in Global.asax) the Application_AuthenticateRequest event handler to dynamically create and assign custom roles to a user.

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        if (HttpContext.Current.User != null)
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                if (HttpContext.Current.User.Identity is FormsIdentity)
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;
                    // Get the stored user-data, in this case, our roles
                    string userData = ticket.UserData;
                    string[] roles = userData.Split(',');
                    HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);

    Best Regards,

    Candice Zhou

    Monday, December 7, 2015 8:25 AM
  • User36582938 posted

    Why repeat code when that is not the title of the question ! u basically repeated code

    Sunday, December 13, 2015 7:46 AM
  • User614698185 posted

    Hi buckd,

    Do you mean you want to custom user properties in identity?

    If so, for example, you could add additional fields - Age, City and Country in the Model.

    public class Model
            //add additional fields
            public int Age { get; set; }
            public string  City { get; set; } 
            public string  Country { get; set; }

    Then, refresh the database.

    Best Regards,

    Candice Zhou

    Monday, December 14, 2015 9:49 AM
  • User36582938 posted

    Ur answer are very poor and not to the point wont be marking as the answer.

    Monday, December 14, 2015 12:15 PM
  • User614698185 posted

    Hi buckd30,

    According to your title, you want to add user properties using database first. My previous reply has told how to add properties using database first, do you really understand my reply?

    Best Regards,

    Candice Zhou

    Tuesday, December 15, 2015 8:00 AM