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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/********************************************************
* Copyright (C) 2007 Andrew Arnott
* Released under the New BSD License
* License available here: http://www.opensource.org/licenses/bsd-license.php
* For news or support on this file: http://blog.nerdbank.net/
********************************************************/
using System;
using System.Globalization;
using System.Net.Mail;
using DotNetOpenId.Extensions;
using System.Xml.Serialization;
using DotNetOpenId.RelyingParty;
using DotNetOpenId.Provider;
using System.Collections.Generic;
namespace DotNetOpenId.Extensions.SimpleRegistration
{
#pragma warning disable 0659, 0661
/// <summary>
/// A struct storing Simple Registration field values describing an
/// authenticating user.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2218:OverrideGetHashCodeOnOverridingEquals"), Serializable()]
public sealed class ClaimsResponse : IExtensionResponse
{
string typeUriToUse;
/// <summary>
/// Creates an instance of the <see cref="ClaimsResponse"/> class.
/// </summary>
[Obsolete("Use ClaimsRequest.CreateResponse() instead.")]
public ClaimsResponse() : this(Constants.sreg_ns) {
}
internal ClaimsResponse(string typeUriToUse) {
if (string.IsNullOrEmpty(typeUriToUse)) throw new ArgumentNullException("typeUriToUse");
this.typeUriToUse = typeUriToUse;
}
/// <summary>
/// The nickname the user goes by.
/// </summary>
public string Nickname { get; set; }
/// <summary>
/// The user's email address.
/// </summary>
public string Email { get; set; }
/// <summary>
/// A combination of the user's full name and email address.
/// </summary>
public MailAddress MailAddress
{
get
{
if (string.IsNullOrEmpty(Email)) return null;
if (string.IsNullOrEmpty(FullName))
return new MailAddress(Email);
else
return new MailAddress(Email, FullName);
}
}
/// <summary>
/// The full name of a user as a single string.
/// </summary>
public string FullName { get; set; }
/// <summary>
/// The user's birthdate.
/// </summary>
public DateTime? BirthDate { get; set; }
/// <summary>
/// The gender of the user.
/// </summary>
public Gender? Gender { get; set; }
/// <summary>
/// The zip code / postal code of the user.
/// </summary>
public string PostalCode { get; set; }
/// <summary>
/// The country of the user.
/// </summary>
public string Country { get; set; }
/// <summary>
/// The primary/preferred language of the user.
/// </summary>
public string Language { get; set; }
CultureInfo culture;
/// <summary>
/// A combination o the language and country of the user.
/// </summary>
[XmlIgnore]
public CultureInfo Culture
{
get
{
if (culture == null && !string.IsNullOrEmpty(Language))
{
string cultureString = "";
cultureString = Language;
if (!string.IsNullOrEmpty(Country))
cultureString += "-" + Country;
culture = CultureInfo.GetCultureInfo(cultureString);
}
return culture;
}
set
{
culture = value;
Language = (value != null) ? value.TwoLetterISOLanguageName : null;
int indexOfHyphen = (value != null) ? value.Name.IndexOf('-') : -1;
Country = indexOfHyphen > 0 ? value.Name.Substring(indexOfHyphen + 1) : null;
}
}
/// <summary>
/// The user's timezone.
/// </summary>
public string TimeZone { get; set; }
#region IExtensionResponse Members
string IExtension.TypeUri { get { return typeUriToUse; } }
IEnumerable<string> IExtension.AdditionalSupportedTypeUris {
get { return new string[0]; }
}
/// <summary>
/// Adds the values of this struct to an authentication response being prepared
/// by an OpenID Provider.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
IDictionary<string, string> IExtensionResponse.Serialize(Provider.IRequest authenticationRequest) {
if (authenticationRequest == null) throw new ArgumentNullException("authenticationRequest");
Dictionary<string, string> fields = new Dictionary<string, string>();
if (BirthDate != null) {
fields.Add(Constants.dob, BirthDate.ToString());
}
if (!String.IsNullOrEmpty(Country)) {
fields.Add(Constants.country, Country);
}
if (Email != null) {
fields.Add(Constants.email, Email.ToString());
}
if ((!String.IsNullOrEmpty(FullName))) {
fields.Add(Constants.fullname, FullName);
}
if (Gender != null) {
if (Gender == SimpleRegistration.Gender.Female) {
fields.Add(Constants.gender, Constants.Genders.Female);
} else {
fields.Add(Constants.gender, Constants.Genders.Male);
}
}
if (!String.IsNullOrEmpty(Language)) {
fields.Add(Constants.language, Language);
}
if (!String.IsNullOrEmpty(Nickname)) {
fields.Add(Constants.nickname, Nickname);
}
if (!String.IsNullOrEmpty(PostalCode)) {
fields.Add(Constants.postcode, PostalCode);
}
if (!String.IsNullOrEmpty(TimeZone)) {
fields.Add(Constants.timezone, TimeZone);
}
return fields;
}
bool IExtensionResponse.Deserialize(IDictionary<string, string> sreg, IAuthenticationResponse response, string typeUri) {
if (sreg == null) return false;
string nickname, email, fullName, dob, genderString, postalCode, country, language, timeZone;
BirthDate = null;
Gender = null;
sreg.TryGetValue(Constants.nickname, out nickname);
Nickname = nickname;
sreg.TryGetValue(Constants.email, out email);
Email = email;
sreg.TryGetValue(Constants.fullname, out fullName);
FullName = fullName;
if (sreg.TryGetValue(Constants.dob, out dob)) {
DateTime bd;
if (DateTime.TryParse(dob, out bd))
BirthDate = bd;
}
if (sreg.TryGetValue(Constants.gender, out genderString)) {
switch (genderString) {
case Constants.Genders.Male: Gender = SimpleRegistration.Gender.Male; break;
case Constants.Genders.Female: Gender = SimpleRegistration.Gender.Female; break;
}
}
sreg.TryGetValue(Constants.postcode, out postalCode);
PostalCode = postalCode;
sreg.TryGetValue(Constants.country, out country);
Country = country;
sreg.TryGetValue(Constants.language, out language);
Language = language;
sreg.TryGetValue(Constants.timezone, out timeZone);
TimeZone = timeZone;
return true;
}
#endregion
/// <summary>
/// Tests equality of two <see cref="ClaimsResponse"/> objects.
/// </summary>
public static bool operator ==(ClaimsResponse one, ClaimsResponse other) {
if ((object)one == null && (object)other == null) return true;
if ((object)one == null ^ (object)other == null) return false;
return one.Equals(other);
}
/// <summary>
/// Tests inequality of two <see cref="ClaimsResponse"/> objects.
/// </summary>
public static bool operator !=(ClaimsResponse one, ClaimsResponse other) {
return !(one == other);
}
/// <summary>
/// Tests equality of two <see cref="ClaimsResponse"/> objects.
/// </summary>
public override bool Equals(object obj)
{
ClaimsResponse other = obj as ClaimsResponse;
if (other == null) return false;
return
safeEquals(this.BirthDate, other.BirthDate) &&
safeEquals(this.Country, other.Country) &&
safeEquals(this.Language, other.Language) &&
safeEquals(this.Email, other.Email) &&
safeEquals(this.FullName, other.FullName) &&
safeEquals(this.Gender, other.Gender) &&
safeEquals(this.Nickname, other.Nickname) &&
safeEquals(this.PostalCode, other.PostalCode) &&
safeEquals(this.TimeZone, other.TimeZone);
}
static bool safeEquals(object one, object other)
{
if (one == null && other == null) return true;
if (one == null ^ other == null) return false;
return one.Equals(other);
}
}
}
|