summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs
blob: 8edbeedc6631601c8f0dbf436fd20bbfe280462c (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
//-----------------------------------------------------------------------
// <copyright file="DotNetOpenAuthWebConsumer.cs" company="Microsoft">
//     Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.AspNet.Clients {
	using System;
	using System.Collections.Generic;
	using System.Net;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth;
	using DotNetOpenAuth.OAuth.ChannelElements;
	using DotNetOpenAuth.OAuth.Messages;

	/// <summary>
	/// The dot net open auth web consumer.
	/// </summary>
	public class DotNetOpenAuthWebConsumer : IOAuthWebWorker {
		#region Constants and Fields

		/// <summary>
		/// The _web consumer.
		/// </summary>
		private readonly WebConsumer _webConsumer;

		#endregion

		#region Constructors and Destructors

		/// <summary>
		/// Initializes a new instance of the <see cref="DotNetOpenAuthWebConsumer"/> class.
		/// </summary>
		/// <param name="serviceDescription">
		/// The service description.
		/// </param>
		/// <param name="tokenManager">
		/// The token manager.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// </exception>
		/// <exception cref="ArgumentNullException">
		/// </exception>
		public DotNetOpenAuthWebConsumer(ServiceProviderDescription serviceDescription, IConsumerTokenManager tokenManager) {
			if (serviceDescription == null) {
				throw new ArgumentNullException("consumer");
			}

			if (tokenManager == null) {
				throw new ArgumentNullException("tokenManager");
			}

			this._webConsumer = new WebConsumer(serviceDescription, tokenManager);
		}

		#endregion

		#region Public Methods and Operators

		/// <summary>
		/// The prepare authorized request.
		/// </summary>
		/// <param name="profileEndpoint">
		/// The profile endpoint.
		/// </param>
		/// <param name="accessToken">
		/// The access token.
		/// </param>
		/// <returns>
		/// </returns>
		public HttpWebRequest PrepareAuthorizedRequest(MessageReceivingEndpoint profileEndpoint, string accessToken) {
			return this._webConsumer.PrepareAuthorizedRequest(profileEndpoint, accessToken);
		}

		/// <summary>
		/// The process user authorization.
		/// </summary>
		/// <returns>
		/// </returns>
		public AuthorizedTokenResponse ProcessUserAuthorization() {
			return this._webConsumer.ProcessUserAuthorization();
		}

		/// <summary>
		/// The request authentication.
		/// </summary>
		/// <param name="callback">
		/// The callback.
		/// </param>
		public void RequestAuthentication(Uri callback) {
			var redirectParameters = new Dictionary<string, string> { { "force_login", "false" } };
			UserAuthorizationRequest request = this._webConsumer.PrepareRequestUserAuthorization(
				callback, null, redirectParameters);
			this._webConsumer.Channel.PrepareResponse(request).Send();
		}

		#endregion
	}
}