This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 866
/
IdentityResult.cs
67 lines (60 loc) · 3.01 KB
/
IdentityResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Identity
{
/// <summary>
/// Represents the result of an identity operation.
/// </summary>
public class IdentityResult
{
private static readonly IdentityResult _success = new IdentityResult { Succeeded = true };
private List<IdentityError> _errors = new List<IdentityError>();
/// <summary>
/// Flag indicating whether if the operation succeeded or not.
/// </summary>
/// <value>True if the operation succeeded, otherwise false.</value>
public bool Succeeded { get; protected set; }
/// <summary>
/// An <see cref="IEnumerable{T}"/> of <see cref="IdentityError"/>s containing an errors
/// that occurred during the identity operation.
/// </summary>
/// <value>An <see cref="IEnumerable{T}"/> of <see cref="IdentityError"/>s.</value>
public IEnumerable<IdentityError> Errors => _errors;
/// <summary>
/// Returns an <see cref="IdentityResult"/> indicating a successful identity operation.
/// </summary>
/// <returns>An <see cref="IdentityResult"/> indicating a successful operation.</returns>
public static IdentityResult Success => _success;
/// <summary>
/// Creates an <see cref="IdentityResult"/> indicating a failed identity operation, with a list of <paramref name="errors"/> if applicable.
/// </summary>
/// <param name="errors">An optional array of <see cref="IdentityError"/>s which caused the operation to fail.</param>
/// <returns>An <see cref="IdentityResult"/> indicating a failed identity operation, with a list of <paramref name="errors"/> if applicable.</returns>
public static IdentityResult Failed(params IdentityError[] errors)
{
var result = new IdentityResult { Succeeded = false };
if (errors != null)
{
result._errors.AddRange(errors);
}
return result;
}
/// <summary>
/// Converts the value of the current <see cref="IdentityResult"/> object to its equivalent string representation.
/// </summary>
/// <returns>A string representation of the current <see cref="IdentityResult"/> object.</returns>
/// <remarks>
/// If the operation was successful the ToString() will return "Succeeded" otherwise it returned
/// "Failed : " followed by a comma delimited list of error codes from its <see cref="Errors"/> collection, if any.
/// </remarks>
public override string ToString()
{
return Succeeded ?
"Succeeded" :
string.Format("{0} : {1}", "Failed", string.Join(",", Errors.Select(x => x.Code).ToList()));
}
}
}