ASP.NET Membership - is this good design - asp.net

I added asp.net membership in my web application. Now I have one table where I have columns for user specific information UserProfile(FirstName, Lastname, DisplayName etc.). I didn't use Profile from membership because if in the future I decide to change asp.net membership for something else I want to keep user personal data in separate table. My question is: I use UserId (unique identifier from asp_membership_users table) as a foreign key in my custom table. Is UserId smart choice to connect these tables?

Yes, it's the primary key of the asp_membership_users table so it makes sense to use it as the foreign key of your custom table.

Related

aspnet_user table for storing customer information

When regsitering in my site (ASP.Net MVC application), the users get inserted into the aspnet_users table. Since its a shopping site, I would want the users to have a customer id and all their details provided by them at registration in this Customer table as well. How do I link these 2 tables? Is it recommended to use the aspnet_user's UserId(Guid) in the application for other business processes.
Also, I would like to know when should a new record be inserted into the customers table.
I mean, when should a new customer be created. I guess its not good to create a record as ans when users are registered? Here, I want to know whats the norm? I felt it would be better to add it when a user adds an item to the shopping cart. Pls guide me.
Thanks in advance.
Add the UserId field into your customer table and then make a foreign key relationship back to the UserId in the aspnet_users table if you want to enforce relational integrity.
I'm not sure what you mean about when to insert the customer record. As long as you insert it after you have created the user (so that you have the user ID), you should be fine. It can happen in the same postback.
I'm not sure how you are saving the user. As in are you using one of the built-in ASP.Net controls or making the call manually?
If you are using the Membership provider as it sounds like you are, you can save the member using:
var user = Membership.CreateUser;
Guid userKey = user.ProviderUserKey;
//Populate your customer object.
//now use whatever EF/ADO/etc... to save your customer record.

Adding custom fields on asp net membership users table

I have a application which I would like to add custom fields to the users table. I am not sure how to work after adding the columns. The columns are foreign keys to another table which holds more details for the user.
I am using Linq-to-SQL. So every time I add a user (using the membership functions i.e. Membership.CreateUser(..)) I end up calling another service to update the foreign keys on the users table.
Any better way of doing this will be highly appreciated.
Why are you adding foreign keys to the User table, pointing to another table with additional info??
I would do it the other way around:
create your own table UserInfo
create a FK column in UserInfo that points to the row in your ASP.NET membership User table
leave the system-provided User table alone - so you won't run into problems when e.g. an upgrade to the ASP.NET membership system is rolled out.....
I strongly recommend you not to extend secure tables such like Users, Membership, Profile. Better create another one table in your database (not secure database) with full info which you need. Call it 'User' with foreign key to 'Id' of table User in secure database.

What is aspnet_Users and aspnet_Membership? Which one should I use? What is the difference?

I'm using default membership class in my website. But I'm a little confused with the complex database design of the ASPNETDB.
Let's say I want to add details about user like name, surname, address etc... Where should I put them aspnet_Membership table or aspnet_Users table and why? I mean I can see that I should put the details on aspnet_Membership. But then again why there is two of them and the bindings on aspnet_Users...
Or lets say I'm adding a messaging into that which table should I key to, for the user details?
I'm confused and I would be appreciated if you can tell me the use of these two tables.
Here is the diagram for the default ASPNETDB which you should probably have:
Rather than modifying the built in ASP.NET membership schema, you can use ASP.NET profiles to store custom properties about your users. Here's one tutorial on the subject.
You can add the profile tables to your schema using aspnet_regsql.exe (which you're probably already familiar with, given that you have already generated most of the schema). Use the -R p switch to get the profile tables/procs.
If you want to connect your custom table to ASP.NET membership, use UserId as foreign key from aspnet_Users table rather than aspnet_Membership table to your custom table. For reference, please note how aspnet_Profile or even aspnet_Membership is using UserId as foreign key from aspnet_Users table.
Please see the database diagram here:
http://msdn.microsoft.com/en-us/library/Aa478948.asp2prvdr0102l(l=en-us).gif
Good luck ...
I would advise not to alter any of the tables provided by the ASP.NET membership schema. To store additional data use the already mentioned ASP.NET profiles option or create a custom one which maybe fits your bill beter.
The reason why there are two tables for storing user data is already answered over here.
Good luck which choosing the right solution.

Adding an integer ID to ASP.NET Forms Authentication

In the standard forms authentication, users are identified by a Guid. I want to give my users an UserId of type int (doesn't have to be the primary key, just something to do lookup's on).
Is it safe to add an additional column to the aspnet_users table, or should I create a new table which FKs to the UserId column and has a Unique column which generates the integer ID?
The later sounds like a bad performance hit to take just for the sake of an int!
EDIT
I want to create URLs like those on stackoverflow. eg. https://stackoverflow.com/users/23590/greg-b where the User ID is an int. For that reason I don't want to use Guids.
I'd create profiles and store the associated urlID there. Web Forms don't have Profiles available out of the box, but you can see a workaround here:
http://www.codersbarn.com/post/2008/06/01/ASPNET-Web-Site-versus-Web-Application-Project.aspx
The advantage of using Profiles is that you can tap into all the existing logic and won't have to write as much custom code yourself, aside from constructing the URL.
You could combine this with Routing for friendly URLs, if you're using ASP.NET 3.5 or up.
UPDATE: kinda similar question:
Shorter GUID using CRC

asp.net user login question

In an asp.net webform app, I need to restrict the logged in user to show data only for their company. This is a primary key of my topmost table. How to I set that based off the user. I imagine I would have another table with some user ID mapped to company ID?
If a user can be part of exactly one company, then generally there would be a table of companies and a table of users, with the latter having a company ID foreign key column to the primary key of the former. The user object would then have the company ID throughout the application (such as in the cookie data or session data) and the application would apply filtering rules accordingly.

Resources