Associate MembershipUser with Another Class - c#

I am working on a project where I am using the membershipuser for authentication, but based on the role of the user I am going to need additional information (different for each role). I know I can extend the membership user in different ways, but I am also wondering if I should just create another class for these additional fields and just use the guid of the membershipuser to find this information. Any thoughts on this would be appreciated.
Wade

Based on tag in OP, you are using legacy ASP.Net Membership Provider or ASP.Net Universal Provider.
If so, the easiest way will be to create separate table. Then add foreign key to your tables.
Note: If you add columns to Membership tables, you will need to create Custom Membership Provider. In other words, Default Membership Provider will not work if you add columns to their tables.
If you are developing new application, you might want to look at ASP.Net Identity 2 which allows to add custom columns to their tables.

Related

Is it possible to implement user profiles without using SimpleMembership?

I need some advice. I'm currently using MVC 4 & SimpleMemberhip with LDAP to authenticate users. The issue is, I don't want to store their usernames and passwords in the
webpages_Membership table due to security concerns. The second issue is I want to provide user-editable profiles.
Here's what works so far:
User logs for the first time and a new entry is created in webpages_Membership
An individualized link to edit the user profile is displayed on the homepage
Username is added to the UserProfiles table when profile is accessed for the first time
Certain user details are fetched from LDAP server and written to profile
Users can then customize their profiles
I'm currently using SimpleMembership with an override to the ValidateUser method. Everything works as it should but I don't need to store the LDAP usernames & passwords. Can this be done?
p.s. I know there is a better way to create new users & profiles besides on first time log in but I'm still working on it.
If you don't want to store the passwords (which SimpleMembership would do by default), you are better off deriving your own custom provider from ExtendedMembershipProvider (or maybe from SimpleMembership, but that would get complex) and write the LDAP implementation, or using one of the ones on NuGet. There's no built-in LDAP support in SimpleMembership, so any approach you do would be a nasty hack which will probably bite you later on.
As for the UserProfile, it doesn't sound like your requirement is that different to the usual UserProfile use case - create custom properties on the UserProfile model, update the database accordingly, and build a UI to allow the user to edit whichever of those properties they should be able to directly edit.
(edit)
Footnote. In my answer to "How do I use my own database with SimpleMembership and WebSecurity? What is MVC4 security all about?" I examine the history of membership, how ExtendedMembershipProvider fits into this, and how the new classes such as WebSecurity work on the basis of a provider being a concrete implementation of ExtendedMembershipProvider (which SimpleMembershipProvider is, for example). For anyone looking to derive their own provider to use with WebSecurity, that answer is worth reading.
I've managed to bypass storing user details in the Membership provider by creating the required tables with Code First. I'm now able to create new users and store them in the UserProfile table.

How can i extend Users table of ASP.Net 4 membership

I want to extend the users table of ASP.Net 4 memberships as below picture
So when the user register on the website i can control the extra columns ("WaitingApprovalRevenue", "ApprovedRevenue" and "WithdrawnRevenue")
Or i should play with default asp.net users table ?!!
You can use the Profile provider, but there is a drawback to the default ASP.NET one where it basically stores all the values in a single field. This makes it hard when you have something outside of the ASP.NET environment which needs to use those values (eg. for reporting purposes).
If that's not a problem, use Profile as Tim has linked, otherwise I would create a separate table with the user GUID as a key and just link it that way.

MVC 3 Code first First with built-in membership authentication

I would like to use code first EF 4.1 to build my application. I'm using MVC 3. I would simultaneously like to use Membership authentication. The problem I see mainly is how do I do a foreign key to the membership users table that was generated by the tool, to the model i'm creating via code.
for example:
dbo.aspnet_Users which has a guid as it's PK
I want to create a user in MY user table (dbo.Users) with the same GUID as my PK.
Is there a way to form that association without it just being implied by the matching data?
I'm not quiete sure what you are trying to achieve. If you want to associate custom data to a user, you could use the profile.
Is there a special reason why you like to sort of "douplicate" some user information?

How can I add relationships to the aspnetdb database in MVC?

I'm having a hard time wrapping my head around how to use the Memberships in MVC. I know there is the built in ASPNETDB database which has all the basic tables for users and such. But what if I wanted to add a relationship between one of my custom tables and this built in user table?
If I had a database table that contained blog comments, we'll call it Comment. And each Comment had a userID associated with it could I do something like?
User.Comments.Add(someCommentObj)
Anyone know of a good article on this? Is this even possible?
Thanks
Have a look at this extensive article on the MembershipProvider:
https://web.archive.org/web/20211020202857/http://www.4guysfromrolla.com/articles/120705-1.aspx
Look at Part 6 and 7, you'll probably want to implement a custom ProfileProvider and store the comment reference in the Profile.
Part 6 - capture additional user-specific information using the
Profile system. Learn about the
built-in SqlProfileProvider.
Part 7 - the Membership, Roles, and Profile systems are all build using
the provider model, which allows for
their implementations to be highly
If you want to use your own custom membership tables then you'll need to build your own MembershipProvider. Matt Wrock has a walkthrough:
You'll notice that the default AccountModel allows you to inject your own provider:
public AccountMembershipService(MembershipProvider provider)
{
_provider = provider ?? Membership.Provider;
}
Nerdinner has an example of dependency injection that you would probably find useful:
Warning Here are two solutions that will work. The first one is easy. The 2nd one, I think is what you're after, but take it for what it's worth. Make sure you realize what you're doing, since this will take the membership provider data and access it directly, which could result in some hidden bombs if you're not careful (like deleting data).
The membership data is meant to just be used for authenticating; roles for authorizing; profiles for user speicific data (like time zone or favorite color.
Solution One
If you wanted to add a comment under the current user (or any user) you could do:
var comment = new Comment(....);
comment.userId = User.Identity.Name; //for user name
or
comment.userId = new Guid(Membership.GetUser().ProviderUserKey); //for guid in table
That's the eays way and you never really have direct access to the aspnet tables, you just use its info.
Solution Two
I'm assuming that you're using L2S and the designer in VS.
By adding the membership table(s) to your L2S design, you will get access to its data. This may even be preferable for some quick querying (like dates, lock out info, etc. since you don't have to use the built-in sprocs which have some serious over-kill and heavy code). If you create a relationship in the DB or in the L2S designer, you'll have a relationship that you can access like your question asks.
At this point, the designer has created your classes for comments and users. If you do anything with the actual user table you just created, you're circumventing the membership provider's design - don't do this unless you now what you're doing. When you add a comment to the User, it will add the comment to the comments tables with the correct relationship intact.
You should now be able to do:
var user = MyUser.GetById(userId);
user.Comments.Add(comment);
Remember, that the User in this case is different than when you do
var user = Membership.GetUser(userId);

Multiple Security Questions in ASP.NET Membership

We are using the ASP.NET Membership Provider for managing the users in our application.
All was fine until when we had a new requirement.
Users should be able to select multiple security questions and give answers to the same.
While recovering the password, the user will be presented with one of the security questions and if the user answers correctly the password will be sent.
By default ASP.NET Membership provides only one security question and one security answer.
Is there is any way to make it use multiple ones?
Creating your own custom ASP.NET membership provider would be the key. Additionally, you'd have to create a custom template for the .NET login controls that use the password Q's, so that you can display multiple question/answer sections.
You may have to set the membership provider to not use the password question/answer, but programmably manage it yourself. By default, it uses that single Q/A to manage security; but since you need multiple, it may be easier to use custom logic to manage this.
EDIT: the only other thing I can think of is have two provider definitions, each with their own password question/answer, so that you are effectively storing two records in the database per user. The thing is that the UI controls won't work with that approach, so you would need to custom build the UI.
HTH.
You could customize the default ASP.NET Membership provider to your specific requirements by subclassing it.

Categories