Skip to content

Commit 7938ce9

Browse files
committed
Working demo of authentication using OpenId via DotNetOpenAuth
0 parents  commit 7938ce9

File tree

244 files changed

+220493
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

244 files changed

+220493
-0
lines changed

openId.wrapdesc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: openId
2+
depends: openwrap anchored content
3+
depends: nunit
4+
depends: dotnetopenauth
5+
depends: jquery
6+
depends: log4net
7+
depends: castle.core
8+
depends: castle.windsor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace OpenIdDemo.AuthenticationProviders.OpenId
2+
{
3+
public class AuthenticationRequest : Interfaces.IAuthenticationRequest
4+
{
5+
public AuthenticationRequest(string url)
6+
{
7+
Url = url;
8+
}
9+
10+
public string Url { get; protected set; }
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace OpenIdDemo.AuthenticationProviders.OpenId
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Web.Mvc;
8+
9+
using DotNetOpenAuth.OpenId.RelyingParty;
10+
11+
public class AuthenticationResponse : Interfaces.IAuthenticationResponse
12+
{
13+
public AuthenticationResponse(Interfaces.OpenIdAuthenticationState state)
14+
{
15+
State = state;
16+
}
17+
18+
public AuthenticationResponse(ActionResult authenticatingActionResult)
19+
{
20+
State = Interfaces.OpenIdAuthenticationState.Authenticating;
21+
AuthenticatingActionResult = authenticatingActionResult;
22+
}
23+
24+
public ActionResult AuthenticatingActionResult { get; protected set; }
25+
public Interfaces.OpenIdAuthenticationState State { get; protected set; }
26+
public Interfaces.IUserIdentity UserIdentity { get; protected set; }
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
namespace OpenIdDemo.AuthenticationProviders.OpenId
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
using DotNetOpenAuth.Messaging;
9+
using DotNetOpenAuth.OpenId;
10+
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
11+
using DotNetOpenAuth.OpenId.RelyingParty;
12+
13+
using log4net;
14+
15+
public class OpenIdAuthenticationProvider : Interfaces.IOpenIdAuthenticationProvider
16+
{
17+
18+
public OpenIdAuthenticationProvider(Interfaces.IFactory factory)
19+
{
20+
_factory = factory;
21+
}
22+
23+
public Interfaces.IAuthenticationResponse Authenticate(Interfaces.IAuthenticationRequest request)
24+
{
25+
var response = _openIdRelyingParty.GetResponse();
26+
if (response == null)
27+
{
28+
return RequestAuthentication(request);
29+
}
30+
31+
return GetUserIdentity(response);
32+
}
33+
34+
#region Private
35+
36+
static private readonly OpenIdRelyingParty _openIdRelyingParty = new OpenIdRelyingParty();
37+
ILog _logger;
38+
Interfaces.IFactory _factory;
39+
40+
private Interfaces.IAuthenticationResponse GetUserIdentity(IAuthenticationResponse response)
41+
{
42+
var identifier = response.ClaimedIdentifier;
43+
var fetch = response.GetExtension<FetchResponse>();
44+
Interfaces.IUserIdentity userIdentity = (fetch == null)
45+
? null
46+
: new UserIdentity(response.ClaimedIdentifier.ToString(),
47+
fetch.GetAttributeValue(WellKnownAttributes.Name.First),
48+
fetch.GetAttributeValue(WellKnownAttributes.Name.Last),
49+
fetch.GetAttributeValue(WellKnownAttributes.Contact.Email));
50+
51+
return _factory.AuthenticationResponse(userIdentity);
52+
}
53+
54+
private Interfaces.IAuthenticationResponse RequestAuthentication(Interfaces.IAuthenticationRequest request)
55+
{
56+
Identifier id;
57+
if (!Identifier.TryParse(request.Url, out id))
58+
{
59+
_logger.Info(string.Format("OpenID Error...invalid url. url='{0}'", request.Url));
60+
return _factory.AuthenticationResponse(Interfaces.OpenIdAuthenticationState.Errored);
61+
}
62+
63+
try
64+
{
65+
var authenticationRequest = _openIdRelyingParty.CreateRequest(request.Url);
66+
var fetch = new FetchRequest();
67+
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
68+
fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
69+
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
70+
authenticationRequest.AddExtension(fetch);
71+
72+
return _factory.AuthenticationResponse(authenticationRequest.RedirectingResponse.AsActionResult());
73+
}
74+
catch (ProtocolException ex)
75+
{
76+
_logger.Error("OpenID Exception...", ex);
77+
return _factory.AuthenticationResponse(Interfaces.OpenIdAuthenticationState.Errored);
78+
}
79+
}
80+
81+
#endregion
82+
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{FB86ED44-2C34-4F9E-AAFC-03BEDDD22065}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>OpenIdDemo.AuthenticationProviders.OpenId</RootNamespace>
12+
<AssemblyName>OpenIdDemo.AuthenticationProviders.OpenId</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="AuthenticationRequest.cs" />
44+
<Compile Include="AuthenticationResponse.cs" />
45+
<Compile Include="OpenIdAuthenticationProvider.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
<Compile Include="UserIdentity.cs" />
48+
</ItemGroup>
49+
<Import Project="..\..\wraps\openwrap\build\OpenWrap.CSharp.targets" />
50+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
51+
Other similar extension points exist, see Microsoft.Common.targets.
52+
<Target Name="BeforeBuild">
53+
</Target>
54+
<Target Name="AfterBuild">
55+
</Target>
56+
-->
57+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("OpenIdDemo.AuthenticationProviders.OpenId")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("OpenIdDemo.AuthenticationProviders.OpenId")]
13+
[assembly: AssemblyCopyright("Copyright © 2011")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("98bd5b56-4953-451c-8390-ce336118f53d")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace OpenIdDemo.AuthenticationProviders.OpenId
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
public class UserIdentity : Interfaces.IUserIdentity
9+
{
10+
public UserIdentity(string identifier, string firstName, string lastName, string emailAddress)
11+
{
12+
Identifier = identifier;
13+
FirstName = firstName;
14+
LastName = lastName;
15+
EmailAddress = emailAddress;
16+
}
17+
18+
public string Identifier { get; protected set; }
19+
public string FirstName { get; protected set; }
20+
public string LastName { get; protected set; }
21+
public string EmailAddress { get; protected set; }
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
namespace OpenIdDemo.AuthenticationProviders.OpenId
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
using DotNetOpenAuth.Messaging;
9+
using DotNetOpenAuth.OpenId;
10+
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
11+
using DotNetOpenAuth.OpenId.RelyingParty;
12+
13+
using log4net;
14+
15+
public class AuthenticationProvider : Interfaces.IAuthenticationProvider
16+
{
17+
18+
public AuthenticationProvider(Interfaces.IFactory factory, OpenIdRelyingParty openIdRelyingParty)
19+
{
20+
Factory = factory;
21+
_openIdRelyingParty = openIdRelyingParty;
22+
}
23+
24+
public Interfaces.IFactory Factory { get; protected set; }
25+
26+
public Interfaces.IAuthenticationResponse Authenticate(Interfaces.IAuthenticationRequest request)
27+
{
28+
var response = _openIdRelyingParty.GetResponse();
29+
if (response == null)
30+
{
31+
return RequestAuthentication(request);
32+
}
33+
34+
return GetUserIdentity(response);
35+
}
36+
37+
#region Private
38+
39+
private readonly OpenIdRelyingParty _openIdRelyingParty;
40+
ILog _logger;
41+
42+
private Interfaces.IAuthenticationResponse GetUserIdentity(IAuthenticationResponse response)
43+
{
44+
var identifier = response.ClaimedIdentifier;
45+
var fetch = response.GetExtension<FetchResponse>();
46+
Interfaces.IUserIdentity userIdentity = (fetch == null)
47+
? null
48+
: Factory.GetUserIdentity(response.ClaimedIdentifier.ToString(),
49+
fetch.GetAttributeValue(WellKnownAttributes.Name.First),
50+
fetch.GetAttributeValue(WellKnownAttributes.Name.Last),
51+
fetch.GetAttributeValue(WellKnownAttributes.Contact.Email));
52+
53+
return Factory.AuthenticationResponse(userIdentity);
54+
}
55+
56+
private Interfaces.IAuthenticationResponse RequestAuthentication(Interfaces.IAuthenticationRequest request)
57+
{
58+
Identifier id;
59+
if (!Identifier.TryParse(request.Url, out id))
60+
{
61+
_logger.Info(string.Format("OpenID Error...invalid url. url='{0}'", request.Url));
62+
return Factory.AuthenticationResponse(Interfaces.AuthenticationState.Errored);
63+
}
64+
65+
try
66+
{
67+
var authenticationRequest = _openIdRelyingParty.CreateRequest(request.Url);
68+
var fetch = new FetchRequest();
69+
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
70+
fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
71+
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
72+
authenticationRequest.AddExtension(fetch);
73+
74+
var actionResult = authenticationRequest.RedirectingResponse.AsActionResult();
75+
return Factory.AuthenticationResponse(actionResult);
76+
}
77+
catch (ProtocolException ex)
78+
{
79+
_logger.Error("OpenID Exception...", ex);
80+
return Factory.AuthenticationResponse(Interfaces.AuthenticationState.Errored);
81+
}
82+
}
83+
84+
#endregion
85+
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace OpenIdDemo.AuthenticationProviders.OpenId
2+
{
3+
public class AuthenticationRequest : Interfaces.IAuthenticationRequest
4+
{
5+
public AuthenticationRequest(string url)
6+
{
7+
Url = url;
8+
}
9+
10+
public string Url { get; protected set; }
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace OpenIdDemo.AuthenticationProviders.OpenId
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Web.Mvc;
8+
9+
using DotNetOpenAuth.OpenId.RelyingParty;
10+
11+
public class AuthenticationResponse : Interfaces.IAuthenticationResponse
12+
{
13+
public AuthenticationResponse(Interfaces.AuthenticationState state)
14+
{
15+
State = state;
16+
}
17+
18+
public AuthenticationResponse(ActionResult authenticatingActionResult)
19+
{
20+
State = Interfaces.AuthenticationState.Authenticating;
21+
AuthenticatingActionResult = authenticatingActionResult;
22+
}
23+
24+
public AuthenticationResponse(Interfaces.IUserIdentity userIdentity)
25+
{
26+
State = Interfaces.AuthenticationState.Authenticated;
27+
UserIdentity = userIdentity;
28+
}
29+
30+
public ActionResult AuthenticatingActionResult { get; protected set; }
31+
public Interfaces.AuthenticationState State { get; protected set; }
32+
public Interfaces.IUserIdentity UserIdentity { get; protected set; }
33+
}
34+
}

0 commit comments

Comments
 (0)