Hello, I recently just started working with ASP.NET MVC Framework. I noticed that as soon as I created an ASP.NET MVC Application, Visual Studio generated a few files which include the handling of user authentication, with registration, signing in, change
password and so on. Can this be modified to use a database with an already existing table of users, with my own fields? Being new, I'm still a little confused about how this is working. I already have Linq to Sql classes and linq functions to get user credentials
from the database and such.
You should be able to do that by just modifying the FormsAuthenticationService and AccountMembershipService in the AccountController that's created by the default project template.
MVC uses the standard ASP.Net membership provider. If you click the Log On button/register link - you can add a user. ASP.Net will create App_Data\ASPNETDB.MDF containing the membership information. You can move ASPNETDB.MDF to a remote DB server, but you
must use the Membership provider schema/DB (unless you write your own provider).
The default authentication/authorization configuration added use ASP.NET standard membership and role manager service provider( the local SQL Express one). If your existing database(for user login) is also ASP.NET standard sql membership database, you can
simply change the connection string of the membership section. Otherwise, you might need to create a custom membership provider which uses your own database tables for user authentication.
I've spend some time reading about custom providers and started creating one. However, I'm still unsure if this is the right way. For instance, the abstract class MembershipProvider has the following method:
public abstract MembershipUser CreateUser( string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status );
How do I go about including more user details such as name, surname, age, etc... if I cannot modify the parameters of that?
Okay, I see that I can implement a custom ProfileProvider for that. So that's good.
One last (hopefully) question. So I'm creating a ShoppingCart, and easily I can add the '[Authorize]' attribute to disallow anonymous users from accessing the page. Now I am somewhat confused how to properly implement a way for a user.
Usually, without using the ASP.NET provider model, I would save the user id in a session variable, and use that id to access the appropriate shopping cart from the database. With the provider model, do I still save the user id in a session variable? Do I
add the user id to a custom MembershipUser so that I can retrieve it with Membership.GetUser() ? Or perhaps am I missing the proper way of doing this?
Thanks
---
Edit: Okay, I think I found the way. MembershipUser.ProviderUserKey.
I added a user to my asp.net configuration and can not find ASPNETDB.MDF in my project. I am using Visual Studio 2008 and 2010. I have also been looking for a script that would build such a database so I could publish it on godaddy. I looked in Server Management
Studio and can't find the database either. What am I missing?
As I stated, I added a user. ASPNETDB.MDF gets created but I have no way of accessing it or getting it over to godaddy. When I try and add it using the SQL Server Management Studio it complains about not being able to open because of permissions. I can't
add any tables to the ASPNETDB.MDF when I am in Visual Studio. My question is mostly about access to ASPNETDB.MDF and get the data over to godaddy.
abcfantasy
Member
3 Points
6 Posts
User Authentication in ASP.NET MVC
Mar 18, 2009 09:10 PM|LINK
Hello, I recently just started working with ASP.NET MVC Framework. I noticed that as soon as I created an ASP.NET MVC Application, Visual Studio generated a few files which include the handling of user authentication, with registration, signing in, change password and so on. Can this be modified to use a database with an already existing table of users, with my own fields? Being new, I'm still a little confused about how this is working. I already have Linq to Sql classes and linq functions to get user credentials from the database and such.
Any help will be much appreciated.
Thanks.
database
jeloff
Contributor
2493 Points
432 Posts
Microsoft
Re: User Authentication in ASP.NET MVC
Mar 18, 2009 11:09 PM|LINK
Hi
You should be able to do that by just modifying the FormsAuthenticationService and AccountMembershipService in the AccountController that's created by the default project template.
Jacques
sukhovilin
Member
4 Points
5 Posts
Re: User Authentication in ASP.NET MVC
Mar 18, 2009 11:22 PM|LINK
You need to create a custom membership provider.
But only good programmers write code that humans can understand.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: User Authentication in ASP.NET MVC
Mar 19, 2009 12:18 AM|LINK
MVC uses the standard ASP.Net membership provider. If you click the Log On button/register link - you can add a user. ASP.Net will create App_Data\ASPNETDB.MDF containing the membership information. You can move ASPNETDB.MDF to a remote DB server, but you must use the Membership provider schema/DB (unless you write your own provider).
A few MVC links to get you started.
Steven Cheng...
Contributor
4199 Points
548 Posts
Microsoft
Moderator
Re: User Authentication in ASP.NET MVC
Mar 19, 2009 07:32 AM|LINK
The default authentication/authorization configuration added use ASP.NET standard membership and role manager service provider( the local SQL Express one). If your existing database(for user login) is also ASP.NET standard sql membership database, you can simply change the connection string of the membership section. Otherwise, you might need to create a custom membership provider which uses your own database tables for user authentication.
#Implementing a Membership Provider
http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx
#Building Custom Providers for ASP.NET 2.0 Membership
http://msdn.microsoft.com/en-us/library/aa479048.aspx
Regards,
Steven
Feedback to us
Microsoft One Code Framework
abcfantasy
Member
3 Points
6 Posts
Re: User Authentication in ASP.NET MVC
Mar 21, 2009 01:05 AM|LINK
Thanks all for the helpful replies.
I've spend some time reading about custom providers and started creating one. However, I'm still unsure if this is the right way. For instance, the abstract class MembershipProvider has the following method:
How do I go about including more user details such as name, surname, age, etc... if I cannot modify the parameters of that?
Thanks again in advance
abcfantasy
Member
3 Points
6 Posts
Re: User Authentication in ASP.NET MVC
Mar 21, 2009 02:57 PM|LINK
Okay, I see that I can implement a custom ProfileProvider for that. So that's good.
One last (hopefully) question. So I'm creating a ShoppingCart, and easily I can add the '[Authorize]' attribute to disallow anonymous users from accessing the page. Now I am somewhat confused how to properly implement a way for a user.
Usually, without using the ASP.NET provider model, I would save the user id in a session variable, and use that id to access the appropriate shopping cart from the database. With the provider model, do I still save the user id in a session variable? Do I add the user id to a custom MembershipUser so that I can retrieve it with Membership.GetUser() ? Or perhaps am I missing the proper way of doing this?
Thanks
---
Edit: Okay, I think I found the way. MembershipUser.ProviderUserKey.
Gordonator
Member
4 Points
2 Posts
Re: User Authentication in ASP.NET MVC
Aug 04, 2010 05:43 PM|LINK
Hi,
I added a user to my asp.net configuration and can not find ASPNETDB.MDF in my project. I am using Visual Studio 2008 and 2010. I have also been looking for a script that would build such a database so I could publish it on godaddy. I looked in Server Management Studio and can't find the database either. What am I missing?
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: User Authentication in ASP.NET MVC
Aug 04, 2010 06:58 PM|LINK
The ASPNETDB.MDF file is not created until you add a user - it's created on the fly.
Gordonator
Member
4 Points
2 Posts
Re: User Authentication in ASP.NET MVC
Aug 04, 2010 07:07 PM|LINK
As I stated, I added a user. ASPNETDB.MDF gets created but I have no way of accessing it or getting it over to godaddy. When I try and add it using the SQL Server Management Studio it complains about not being able to open because of permissions. I can't add any tables to the ASPNETDB.MDF when I am in Visual Studio. My question is mostly about access to ASPNETDB.MDF and get the data over to godaddy.
Thank you,
Gordon