0

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.

4

2 回答 2

2

您需要用户可以输入的唯一内容来在身份验证期间识别自己,无论是用户提出的用户名、电子邮件地址、PIN 等......让用户记住生成的数字作为 UserProfile 的唯一标识符确实似乎不是很好的用户体验。因此,如果您不想添加任何其他列,则可以将用户在登录过程中使用的用于唯一标识该用户的任何内容存储在用户名列中。从最新更新到问题,电子邮件地址似乎被用于识别用户,您可以将其存储在用户名列中而不会出现问题。

向 UserProfile 添加列很容易,如果它提供更好的用户体验和安全性,我不会回避它。例如,如果您想捕获一个显示名称以显示在网站上或与用户的任何通信中,您可以添加一个电子邮件列并告诉 SimpleMembership 您希望通过更改WebSecurity 中的参数来使用它来识别用户。初始化数据库连接方法。您可以将参数userNameColumn更改为电子邮件列的名称。

于 2013-11-14T13:59:32.043 回答
1

是的,要么使用 ID 作为用户名,要么使用唯一的电子邮件作为用户名。顺便说一句,在这种情况下,这不是一种混乱的方式。如果没有人会看到 userId,我会使用 id。

于 2013-11-12T21:56:01.383 回答