Bear in mind, I have researched this and have found several articles, however, they are mostly old (like from 2008) so I am wanting more recent information pertaining to the latest version(s) of ASP.NET MVC.
I am using the Membership built-in thing to provide user registration, login and roles.
I want to use the profile functions, too.
I don’t know how to create a profile and I can’t find any articles on how to do this in MVC 3. I found one using MVC 2, maybe it’s the same, but I want to use the latest available methods.
Can someone show me a step by step solution to creating a profile?
I am considering using my own membership classes + forms authentication. That way, creating profiles is as simple as assigning a foreign key…
What’s the experts’ opinion?
Please provide your answer in VB and not C# (I don’t know why everyone writes me stuff in C#).
Thanks.
Edit: Here is my Register function:
'
' POST: /Account/Register
<HttpPost()> _
Public Function Register(ByVal model As RegisterModel) As ActionResult
If ModelState.IsValid Then
' Attempt to register the user
Dim createStatus As MembershipCreateStatus
Membership.CreateUser(model.UserName, model.Password, model.Email, Nothing, Nothing, True, Nothing, createStatus)
If createStatus = MembershipCreateStatus.Success Then
FormsAuthentication.SetAuthCookie(model.UserName, False)
Return RedirectToAction("Index", "Home")
Else
ModelState.AddModelError("", ErrorCodeToString(createStatus))
End If
End If
' If we got this far, something failed, redisplay form
Return View(model)
End Function
Personally, I would recommend against using the SQL Profile Provider (which is what you are using). Nothing has changed with profiles since MVC2 (or, for that matter, since it was introduced in webforms with .NET 2.0).
The reason is this: Profile data is stored as XML in the database, which makes it very difficult to use profile data outside of your app (meaning in a pure SQL query).
You’re probably much better off creating profile fields in your database directly. This way you know which table / column the data is coming from, and can create views if you need to. Otherwise, you would have to parse a table column’s XML to extract the profile data (which is what the ProfileCommon does in .NET).
Reply to comments
First of all, I was wrong about the property name. It is
ProviderUserKey, notProviderKey. However unless you want to store profile properties for anonymous users, you could just as easily useMembershipUser.UserNameas your FK value, since it will also be unique.Here is an example using the UserName instead of the ProviderUserKey. I would recommend this approach if you are not storing profile info for anonymous users: