summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/AuthenticationResult.cs
blob: d5ef1090b8ffd13f6941a26a2ca6995bfcd40c23 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//-----------------------------------------------------------------------
// <copyright file="AuthenticationResult.cs" company="Microsoft">
//     Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.AspNet {
	using System;
	using System.Collections.Generic;
	using System.Diagnostics.CodeAnalysis;
	using DotNetOpenAuth.Messaging;

	/// <summary>
	/// Represents the result of OAuth or OpenID authentication.
	/// </summary>
	public class AuthenticationResult {
		#region Constants and Fields

		/// <summary>
		/// Returns an instance which indicates failed authentication.
		/// </summary>
		[SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes",
			Justification = "This type is immutable.")]
		public static readonly AuthenticationResult Failed = new AuthenticationResult(isSuccessful: false);

		#endregion

		#region Constructors and Destructors

		/// <summary>
		/// Initializes a new instance of the <see cref="AuthenticationResult"/> class.
		/// </summary>
		/// <param name="isSuccessful">
		/// if set to <c>true</c> [is successful]. 
		/// </param>
		public AuthenticationResult(bool isSuccessful)
			: this(isSuccessful, provider: null, providerUserId: null, userName: null, extraData: null) { }

		/// <summary>
		/// Initializes a new instance of the <see cref="AuthenticationResult"/> class.
		/// </summary>
		/// <param name="exception">
		/// The exception. 
		/// </param>
		public AuthenticationResult(Exception exception)
			: this(isSuccessful: false) {
			if (exception == null) {
				throw new ArgumentNullException("exception");
			}

			this.Error = exception;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="AuthenticationResult"/> class.
		/// </summary>
		/// <param name="isSuccessful">
		/// if set to <c>true</c> [is successful]. 
		/// </param>
		/// <param name="provider">
		/// The provider. 
		/// </param>
		/// <param name="providerUserId">
		/// The provider user id. 
		/// </param>
		/// <param name="userName">
		/// Name of the user. 
		/// </param>
		/// <param name="extraData">
		/// The extra data. 
		/// </param>
		public AuthenticationResult(
			bool isSuccessful, string provider, string providerUserId, string userName, IDictionary<string, string> extraData) {
			this.IsSuccessful = isSuccessful;
			this.Provider = provider;
			this.ProviderUserId = providerUserId;
			this.UserName = userName;
			if (extraData != null) {
				// wrap extraData in a read-only dictionary
				this.ExtraData = new ReadOnlyDictionary<string, string>(extraData);
			}
		}

		#endregion

		#region Public Properties

		/// <summary>
		/// Gets the error that may have occured during the authentication process
		/// </summary>
		public Exception Error { get; private set; }

		/// <summary>
		/// Gets the optional extra data that may be returned from the provider
		/// </summary>
		public IDictionary<string, string> ExtraData { get; private set; }

		/// <summary>
		/// Gets a value indicating whether the authentication step is successful.
		/// </summary>
		/// <value> <c>true</c> if authentication is successful; otherwise, <c>false</c> . </value>
		public bool IsSuccessful { get; private set; }

		/// <summary>
		/// Gets the provider's name.
		/// </summary>
		public string Provider { get; private set; }

		/// <summary>
		/// Gets the unique user id that is returned from the provider.
		/// </summary>
		public string ProviderUserId { get; private set; }

		/// <summary>
		/// Gets the user name that is returned from the provider.
		/// </summary>
		public string UserName { get; private set; }
		#endregion
	}
}