aspnet_user table for storing customer information - asp.net

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.

Related

Insert blank record on page load if record does not exist for current user

I'm working on using the membership functionality of the ASP .Net sites. There are multiple ways to create users and one way is to create users and add a user profile to the system. The other is to use an enhanced wizard then add the user and then their profile information. Well, if you go route 1, then the user does not get a record inserted into the user profile table and then when the user goes to update their profile, then on the page load I would like the page to look for their record. If one does not exist, insert a blank one. Does anyone have a sample script to look up a user's profile based on their unique id in SQL CE? If the record does not exist (record count = 0) then insert a new blank record.

performance issues in ASP.NET Membership

I am working with Aspnet membership, and I have stored users information in membership related data e.g. userid and password stored in Aspnet_Membership table, member profile like firstname, lastname, surname, dob etc in aspnet_profile table.
Now I need to add a search panel in my webpage. This search panel contains searching parameters like firstname, lastname and joining date etc. as per this searching parameters I need to search users from profile and user's last login information. I used profilecommon and profilebase class for retrieving this data. But it is seriously slow. The system takes at least 2 minutes to find data as per parameters from 70 records.
Please help to resolve this program.
You should retrieve the membership and profile information within the stored procedure, and not using any of the code-based classes to get the name. It won't perform well with extracting profile information per user.

Display own records in grid view

Im new to asp and i need u guys to guide me pls? Okay, lets just straight to the point. How to display current user who logged in to the system their own data? Like my system is about tracking expenses spent by user monthly. So, once user entered the expenses, prices for that particular month, it will be stored in db of course. But the tricky part here is when the user log in again, they should be able to view back their own records. So, basically i have a drop down menu which is used for month, Jan-Dec, grid view which to view all the particular user's records, view button (when user choose month,they click on the button and the grid view will show the expenses for that particular month) and thats all. So, if you guys have any idea, solution, pls tell me how. I would be appreciated if you could gimme the step by step to do that. Thank you.
|username|Month|Expense1|price 1| Expense2|Price2|....//this is the eg of my db structure
P/s: i dont know how to use HttpContext.Current.User.Identity.Name. If you could guide me by gimme the steps, that would be great! Thank you again.
Firstly, You must be having a Login form to login and view monthly expense and make new entries.
Now you can follow the below steps and make all your application working.
Assuming you have created the database to store new users information with there passwords and expenses table storing data related to the expense with respect to particular user after login.
Steps
once the user is logged in to your application. You can store the
User ID in Session.
This session can help you to take are which user is logged in or there are no users logged in currently to your application
since you have the database filled with all the details related to
expense. After login check if you have any user id in your session
or your session is null or not.
if you find any User ID session just pass this as a SqlParameter to your store Procedure and fetch all the records of this User id.
If you find any records bind your results to grid view. Otherwise display no records found and give a provision to Add new Expense Detail.
If you are using MemberShip API for Maintaining User Account and Login then you can user the HttpContext.Current.User.Identity.Name get the User ID from this name using MemberShip class in System.Web.Security namespace.
Hope the above steps are clear and you follow the functionality properly, to solve your problem.

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.

"Role Management" vs "User Management" in ASP.NET

Question No 1
I am familiar with role management, a particular member in a particular role can do this and access this functionally. What I need to do is Manage individual user, not the role he is in.
For example, lets say I create a role, called "Sales". I setup the role permission what the sales persons can do. Now i want to keep a check on individual user. For example if this is "john", i want to show him the records only he created. If his is peter, I want to show him only that records which he created, not by john or other sales people.
Is there a thing called "User Management" in ASP.NET that we can use? If not we have to create it ourselves and I believe the integration with ASP.NET "Role Management" will not be that smooth.
Question No 2.
I am using control for user login. I want to create a session at this time so I can keep track of which user is signed in so I can show him the records only pertaining to him. How can I do that?
Your Q1 isn't really about Role vs User management (ie: authorizations) at this point. It's about audit tracking within your application.
And the way you do that is you capture the ID of the user who created the record in question with the record, so that later you can filter on that ID.
Pseudo database structure
Table Sales
Field...
Field...
Field...
CreatedByUser int not null, -- Populate this on creation and never change it again
ModifiedByUser int not null - populate this on every row update including insert
See ASP.NET Profile Properties.
Assuming the records in the database correspond to a unique ID for a user, you can store the unique id in a profile property per user.
1) If you want to filter records by the creating user, you need to record in your table the ID of the user who created the record. You can access the name of current user through User.Identity.Name and their ID (provider-dependent) through User.ProviderUserKey.
2) Sessions are created automatically in ASP.NET and provided you have a properly configured MembershipProvider, you can retrieve all the needed user info using the User object as shown above.
It sounds like you are a little unfamiliar with ASP.NET Membership and Roles capabilities, because they are actually set up quite well to accomplish what you are describing. I would recommend checking out this tutorial series:
https://web.archive.org/web/20211020202857/http://www.4guysfromrolla.com/articles/120705-1.aspx
You are talking about Authentication and Authorization. For question 1 you and implement a custom authorization provider to allow for user level control http://msdn.microsoft.com/en-us/library/aa479048.aspx For question 2, once you log in and are Authenticated, the session contains a userprinciple object that has the info in it automatically.

Resources