Search code examples
c#asp.netsqlmembership

ASP.Net Membership and User Managment


My database 'ClinicDB' is Visual studio's local db and i ran 'aspnet_regsql' on it so i have all membership stored procedures and tables and also have my 'client_tbl' and 'doctor_tbl' that i created them to store more data with a 'user_id' field. I'm new in this asp membership technology how can i add user in 'asp_membership' table and also in doctor or client table with the user id that 'asp_membership' generates after creating a user. Should i use membership class?

MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text);

then how can i insert the client or doctor data in their table? should i create the user then retrieve the 'user_Id' and insert it in Client and Doctor table with their data?


Solution

  • You have already answered your own question. This line...

    MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text);
    

    will effectively create the new user in the membership database. Then, notice the object returned from the method call is the newly-created user which exposes a property named ProviderUserKey. This property holds the underlying user id which you will need to cast to whatever data type the membership provider uses usually a GUID or an Integer

    Guid userId = new Guid(newUser.ProviderUserKey.ToString());
    

    or

    int userId = (int)newUser.ProviderUserKey;
    

    Then, you can add the required info to your business-specific tables (Client, Doctor). Another approach will be extending the membership system, but this will require a bit more effort on your side