Question

Users in my database have a non-unique display name; the only unique identifier is the UserId.

Normally, I would add a user to a Role using the following:

Roles.AddUserToRole(user.Username, role);

But now I don't have usernames, so I need to re-think all work related to Roles.

One messy option I can think of is to copy the Id of every user into the Username field just to satisfy SimpleMembershipProvider... though I'd rather somehow use extension methods to handle this if it's even remotely advisable and possible to do so... just so I don't have to clutter my Users table with a bogus column.

Any help here would be much appreciated.

Update:

Even if I copy the userId to the username column to get SimpleMembership working, I still need a username to create the user:

  WebSecurity.CreateUserAndAccount(model.UserName, model.Password, etc)

So I'm at a loss of what to do, aside from rolling my own Membership.

Update again: I just realized I can pluck my email field and just use the Username column to store unique email addresses. I'm still interested in hearing how else this could be addressed.

Was it helpful?

Solution 2

yes, either use the Ids as username or if the email is unique as username. It is not a messy way btw with this situation. I would go with the id, if no one will eversee the userId.

OTHER TIPS

You need something unique the user can enter to identify themselves during authentication, whether it is a username they user came up with, an email address, PIN, etc... Having the user remember a number generated as the unique identifier for the UserProfile does not seem like a good user experience. So whatever is used to uniquely identify that user, that is used during the log in process by the user, can be stored in the username column, if you do not want to add any additional columns. From the latest update to the question it looks like email address is being used to identify the user and you could store this in the username column without issue.

Adding columns to the UserProfile is easy to do and I would not shy away from it if it provides a better user experience and security. For example, if you want to capture a display name to display on the web site or in any communications with users you could add an email column and tell SimpleMembership that you want to use that to identify the user by changing a parameter in the WebSecurity.InitializeDatabaseConnection Method. You would change the parameter userNameColumn to be the name of your email column.

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