Question

I'm trying to create a Custom Simple Membership Provider in ASP.NET MVC 4. In InitializeSimpleMembershipAttribute class, I have this line of code:

WebSecurity.InitializeDatabaseConnection("ConnStringForWebSecurity", "User", "", "Username", autoCreateTables: false);

As you can see here, I try to use User table in my database. For InitializeDatabaseConnection function:

  • The first parameter is the connection string name.
  • The second one is table name.
  • The third one is user id column.
  • The fourth one is username column.

This function expects that we have an id column for User table. But in my case. I use Username column as the primary key, so I don't have id column.

My question is, if I don't have id column, what should I pass to the third parameter? Passing null or empty string is not allowed and exception will be thrown. What should I do?

Thanks a lot for your help.

Was it helpful?

Solution

Short answer -

You have to have ID column (name it as you like) of type int in the user profile table. SimpleMembershipProvider (SMP) mandates two non null column one ID and second user name in the UserProfile table. This is when you choose to derive from SimpleMembershipProvider which in your case I doubt you really need to. If you have any custom needs (which SimpleMembershipProvider stops you to achieve), you can derive from MembershipProvider instead.

The exception is thrown because, SimpleMembershipProvider uses the UserId column internally and it assumed to be not null.

A bit more detail -

Now for a bit more detail to help you understand why so -

SimpleMembershipProvider has been designed to be flexible however its key objective is to provide a simpler way of validating user. Its focus is to target those 80% of the web application that has common Authentication needs.

If you want to customize, for example, you want to add a custom column (email) in your UserProfile table (the table name is customization as well), you can do so. SimpleMembershipProvider simply expects you to have a database created, it can create table for you but the table has to have two mandatory columns (UserID and UserName).

For more information see the link shared in comment by @miller.

If you have any follow up question please share with a bit more details about what you are trying to achieve by creating a custom provider.

Edit (Answer to follow up question)

The name of the column can be anything (EmployeeId will work as well), it will be created as non null int type in database table. Your application will work as long as you are not calling a method that make use of UserId. I wonder how you are creating a new user. With a new MVC 4 web application, when you register a new user, WebSecurity forward the call to SimpleMembershipProvider to create a new user. SimpleMembershipProvider's CreateUserRow() method checks UserID exist or not. If already exist throw an exception. Once the user is created SimpleMembershipProvider uses UserId for several public method e.g. GetUserNameFromId(), GetUser(), DeleteUser(), CreateAccount(), etc.

The way Membership uses UserProfile table, I am afraid you will have to provide a UserId column. You can retrieve user by using UserName as well and will not use UserId at all.

When you use SimpleMembershipProvider consider UserProfile table's two columns as reserved for internal usage. Any custom application logic can be build by adding more column.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top