简体   繁体   English

ASP.NET身份更改验证

[英]ASP.NET Identity Change Validation

I want to use ASP.NET Identity for several websites that i have created, and i do want to use the same database on each of them. 我想将ASP.NET Identity用于创建的多个网站,并且确实要在每个网站上使用相同的数据库。

When i try to register two users with the same email, i get the "Name ****** is already taken." 当我尝试使用相同的电子邮件注册两个用户时,我得到“名称******已被使用”。 I have already set the field "RequireUniqueEmail = false" in IdentityConfig. 我已经在IdentityConfig中设置了字段“ RequireUniqueEmail = false”。

In the AspNetUsers i have a field called "web_id". 在AspNetUsers中,我有一个名为“ web_id”的字段。 It should be possible to create one identical user within one web_id, but it should be possible to create users with the same email on different web_id's. 应该可以在一个web_id中创建一个相同的用户,但是应该可以在不同的web_id上创建具有相同电子邮件的用户。

Someone who knows how i can do this? 谁知道我该怎么做?

Here is the source code of Identity UserValidator, UserValidator . 这是Identity UserValidator和UserValidator的源代码。 You can see if set "RequireUniqueEmail = false", it won't validate email anymore, that's not ideal. 您可以看到是否设置了“ RequireUniqueEmail = false”,它将不再验证电子邮件,这是不理想的。

There are two solutions I can think of: 我可以想到两种解决方案:

  1. Make (email, web_id) as a grouped key/index in db schema, in this case either of them duplicate is OK, but not both. 使(电子邮件,web_id)作为数据库模式中的分组键/索引,在这种情况下,两个都可以重复,但不能两者都重复。 Then you handle the error when creating user, if failed to create user because of this index duplicate, then treat it as Duplicate Email. 然后,您将在创建用户时处理错误,如果由于此索引重复而无法创建用户,则将其视为“重复电子邮件”。 Of course, you need to set "RequireUniqueEmail = false" in this case. 当然,在这种情况下,您需要设置“ RequireUniqueEmail = false”。

  2. Or create a subclass of UserValidator or interface IUserValidator, implement your own logic to check (email, web_id), then set it to UserManager. 或创建UserValidator的子类或接口IUserValidator,实现自己的逻辑检查(电子邮件,web_id),然后将其设置为UserManager。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM