summaryrefslogtreecommitdiffstats
path: root/samples/ProviderPortal/Code/Util.cs
blob: f86497214088149923574ad3a958981f94280044 (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Web;
using DotNetOpenId.Provider;
using ProviderPortal;
using DotNetOpenId;

/// <summary>
/// Summary description for Util
/// </summary>
public class Util {
	public static string ExtractUserName(Uri url) {
		return url.Segments[url.Segments.Length - 1];
	}
	public static string ExtractUserName(Identifier identifier) {
		return ExtractUserName(new Uri(identifier.ToString()));
	}
	public static Identifier BuildIdentityUrl() {
		string username = HttpContext.Current.User.Identity.Name;
		// be sure to normalize case the way the user's identity page does.
		username = username.Substring(0, 1).ToUpperInvariant() + username.Substring(1).ToLowerInvariant();
		return new Uri(HttpContext.Current.Request.Url, "/user/" + username);
	}
	internal static void ProcessAuthenticationChallenge(IAuthenticationRequest idrequest) {
		if (idrequest.Immediate) {
			if (idrequest.IsDirectedIdentity) {
				if (HttpContext.Current.User.Identity.IsAuthenticated) {
					idrequest.LocalIdentifier = Util.BuildIdentityUrl();
					idrequest.IsAuthenticated = true;
				} else {
					idrequest.IsAuthenticated = false;
				}
			} else {
				string userOwningOpenIdUrl = Util.ExtractUserName(idrequest.LocalIdentifier);
				// NOTE: in a production provider site, you may want to only 
				// respond affirmatively if the user has already authorized this consumer
				// to know the answer.
				idrequest.IsAuthenticated = userOwningOpenIdUrl == HttpContext.Current.User.Identity.Name;
			}
		} else {
			HttpContext.Current.Response.Redirect("~/decide.aspx", true);
		}
	}
}