4

我需要使用一些更复杂的规则来验证新(或更新)用户的电子邮件地址。

我想过使用进入用户管理器的用户存储,但是 IdentityResult 是在哪里以及如何构造的?

我只是抛出一个异常,仅此而已?还是有一些额外的验证机制?

4

3 回答 3

11

IdentityResult takes an IEnumerable<String> in the constructor:

public IdentityResult(IEnumerable<string> errors)

These are the errors that will be passed back to the calling code.

Alternatively you can call this method:

var identityResult = IdentityResult.Failed("First error", "Second validation error");

This will be identical to the calling the constructor with list of strings.

于 2015-02-17T14:28:15.950 回答
3

.NETCore 改变了一切。因此,要返回一个失败的结果,您必须在其中分配自己的错误,您必须为IdentityError要传递的每个错误创建一个完全限定的实例。

例如,假设您有以下错误,并且您想用这些错误重新处理失败的结果:

var errors1 = "Your password is incorrect";
var errors2 = "Your email is not recognized";

要继续并为这些错误返回失败的地址,您将执行以下操作:

var result = IdentityResult.Failed(
   new IdentityError[] {
      new IdentityError{
         Code = "0001",
         Description = error1
      },
      new IdentityError{
         Code = "0002",
         Description = error2
      }
   }
);

基本上这里发生的事情是期望的新Failed方法为. 显然,如果您有很多错误,您将创建一个变量,然后将所有错误放入其中,然后将其传递给您的方法。IdentityResultparamsIdentityError[]IdentityError[]Failed

于 2020-09-06T19:32:09.060 回答
1

像这样做 :

var errorMessage = new IdentityError
                {
                    Description = "error message"
                };

                result = 
                    IdentityResult.Failed(errorMessage);
于 2021-06-19T19:06:21.710 回答