Search code examples
c#asp.netasp.net-membershipuser-roles

How do I display information for user accounts?


I have a website I'm developing and have roles setup with one group "admin" that I want to be able to select a user and display information about that user...name, password, security question and so on. What would be the easiest way to get this done? Also, I have modified the default login steps to include a few additional requirements, such as first and last name, company, etc. I would like the "admin" group to be able to view all this information quickly and easily so if a customer from another company calls us saying they fired that person, we can remove the user based on their actual name, not username.

EDIT Can I do something like:

MembershipUser user = System.Web.Security.Membership.GetUser(RegisterUser.UserName);
user.Comment = fnametxt.Text.ToString() + " " + lnametxt.Text.ToString() + " " + companytxt.Text.ToString();
System.Web.Security.Membership.UpdateUser(user);

to store the additional info then recall the user.Comment from the sql database when needed?


Solution

  • It would appear that what you are looking for is a Profile provider. This can be used to store addition information about users such as first and last names and other miscellanea.

    in Web.config

    <profile>
          <providers>
            <clear/>
            <add name="AspNetSqlProfileProvider"
                 type="System.Web.Profile.SqlProfileProvider"
                 connectionStringName="yourConnectionString"<!--Same as membership provider-->
                 applicationName="/"/>
          </providers>
          <properties>
            <add name="FirstName" type="string"/>
            <add name="LastName" type="string"/>
            <add name="Company" type="string"/>
          </properties>
        </profile>
    

    Add/edit profile properties:

    var username = User.Identity.Name;
    ProfileBase profile = ProfileBase.Create(username);
    profile["FirstName"] = "John";
    profile["LastName"] = "Smith";
    profile["Company"] = "WalMart";
    profile.Save();
    

    Read profile properties:

    var username = User.Identity.Name;
    var profile = ProfileBase.Create(username);
    var firstName = profile["FirstName"] as string;
    var lastName = profile["LastName"] as string;
    var company = profile["Company"] as string;
    

    I think this would be the way to go and a bit cleaner and easier to maintain that using comments.