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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
//-----------------------------------------------------------------------
// <copyright file="OpenIdButton.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OpenId.RelyingParty {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Drawing.Design;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using DotNetOpenAuth.Messaging;
/// <summary>
/// An ASP.NET control that renders a button that initiates an
/// authentication when clicked.
/// </summary>
public class OpenIdButton : OpenIdRelyingPartyControlBase {
#region Property defaults
/// <summary>
/// The default value for the <see cref="Text"/> property.
/// </summary>
private const string TextDefault = "Log in with [Provider]!";
/// <summary>
/// The default value for the <see cref="PrecreateRequest"/> property.
/// </summary>
private const bool PrecreateRequestDefault = false;
#endregion
#region View state keys
/// <summary>
/// The key under which the value for the <see cref="Text"/> property will be stored.
/// </summary>
private const string TextViewStateKey = "Text";
/// <summary>
/// The key under which the value for the <see cref="ImageUrl"/> property will be stored.
/// </summary>
private const string ImageUrlViewStateKey = "ImageUrl";
/// <summary>
/// The key under which the value for the <see cref="PrecreateRequest"/> property will be stored.
/// </summary>
private const string PrecreateRequestViewStateKey = "PrecreateRequest";
#endregion
/// <summary>
/// Stores the asynchronously created message that can be used to initiate a redirection-based authentication request.
/// </summary>
private HttpResponseMessage authenticationRequestRedirect;
/// <summary>
/// Initializes a new instance of the <see cref="OpenIdButton"/> class.
/// </summary>
public OpenIdButton() {
}
/// <summary>
/// Gets or sets the text to display for the link.
/// </summary>
[Bindable(true), DefaultValue(TextDefault), Category(AppearanceCategory)]
[Description("The text to display for the link.")]
public string Text {
get { return (string)ViewState[TextViewStateKey] ?? TextDefault; }
set { ViewState[TextViewStateKey] = value; }
}
/// <summary>
/// Gets or sets the image to display.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "Bindable property must be simple type")]
[Bindable(true), Category(AppearanceCategory)]
[Description("The image to display.")]
[UrlProperty, Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public string ImageUrl {
get {
return (string)ViewState[ImageUrlViewStateKey];
}
set {
UriUtil.ValidateResolvableUrl(Page, DesignMode, value);
ViewState[ImageUrlViewStateKey] = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether to pre-discover the identifier so
/// the user agent has an immediate redirect.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Precreate", Justification = "Breaking change to public API")]
[Bindable(true), Category(OpenIdCategory), DefaultValue(PrecreateRequestDefault)]
[Description("Whether to pre-discover the identifier so the user agent has an immediate redirect.")]
public bool PrecreateRequest {
get { return (bool)(ViewState[PrecreateRequestViewStateKey] ?? PrecreateRequestDefault); }
set { ViewState[PrecreateRequestViewStateKey] = value; }
}
/// <summary>
/// Gets or sets a value indicating when to use a popup window to complete the login experience.
/// </summary>
/// <value>The default value is <see cref="PopupBehavior.Never"/>.</value>
[Bindable(false), Browsable(false)]
public override PopupBehavior Popup {
get { return base.Popup; }
set { ErrorUtilities.VerifySupported(value == base.Popup, OpenIdStrings.PropertyValueNotSupported); }
}
/// <summary>
/// When implemented by a class, enables a server control to process an event raised when a form is posted to the server.
/// </summary>
/// <param name="eventArgument">A <see cref="T:System.String"/> that represents an optional event argument to be passed to the event handler.</param>
protected override void RaisePostBackEvent(string eventArgument) {
if (!this.PrecreateRequest) {
this.Page.RegisterAsyncTask(new PageAsyncTask(async ct => {
try {
var requests = await this.CreateRequestsAsync(ct);
var request = requests.First();
await request.RedirectToProviderAsync(new HttpContextWrapper(this.Context), ct);
} catch (InvalidOperationException ex) {
throw ErrorUtilities.Wrap(ex, OpenIdStrings.OpenIdEndpointNotFound);
}
}));
}
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
if (!this.DesignMode) {
ErrorUtilities.VerifyOperation(this.Identifier != null, OpenIdStrings.NoIdentifierSet);
if (this.PrecreateRequest) {
this.Page.RegisterAsyncTask(
new PageAsyncTask(
async ct => {
var requests = await this.CreateRequestsAsync(ct);
this.authenticationRequestRedirect = await requests.FirstOrDefault().GetRedirectingResponseAsync(ct);
}));
}
}
}
/// <summary>
/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Web.UI.HtmlTextWriter.WriteEncodedText(System.String)", Justification = "Not localizable")]
protected override void Render(HtmlTextWriter writer) {
if (string.IsNullOrEmpty(this.Identifier)) {
writer.WriteEncodedText(string.Format(CultureInfo.CurrentCulture, "[{0}]", OpenIdStrings.NoIdentifierSet));
} else {
string tooltip = this.Text;
if (this.PrecreateRequest && !this.DesignMode) {
if (this.authenticationRequestRedirect != null) {
this.RenderOpenIdMessageTransmissionAsAnchorAttributes(writer, this.authenticationRequestRedirect, tooltip);
} else {
tooltip = OpenIdStrings.OpenIdEndpointNotFound;
}
} else {
writer.AddAttribute(HtmlTextWriterAttribute.Href, this.Page.ClientScript.GetPostBackClientHyperlink(this, null));
}
writer.AddAttribute(HtmlTextWriterAttribute.Title, tooltip);
writer.RenderBeginTag(HtmlTextWriterTag.A);
if (!string.IsNullOrEmpty(this.ImageUrl)) {
writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ResolveClientUrl(this.ImageUrl));
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Alt, this.Text);
writer.AddAttribute(HtmlTextWriterAttribute.Title, this.Text);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
} else if (!string.IsNullOrEmpty(this.Text)) {
writer.WriteEncodedText(this.Text);
}
writer.RenderEndTag();
}
}
}
}
|