I'm in a situation where I need to validate a new (or updating) users' email address using some more complex rules.
I thought of using the User store that goes into the user manager but where and how does the IdentityResult get constructed?
Do I just throw an exception and that's it? Or is there some extra validation mechanism?
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.
.NETCore has changed things. So to return a failed result where you assign your own errors you would have to create a fully qualified IdentityError
instance for each error you want to pass.
Let's say for example you have the following errors and you want to returl a failed result with those errors:
var errors1 = "Your password is incorrect";
var errors2 = "Your email is not recognized";
To proceed and return a failed address for with these errors, you would do the following:
var result = IdentityResult.Failed(
new IdentityError[] {
new IdentityError{
Code = "0001",
Description = error1
},
new IdentityError{
Code = "0002",
Description = error2
}
}
);
Basically what's happeneing here is that the new Failed
method of the IdentityResult
expects params
as IdentityError[]
. Obviously, if you had many errors you'd create an IdentityError[]
variable and then put all your errors in and then pass it to your Failed
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With