summaryrefslogtreecommitdiffstats
path: root/samples/OpenIdProviderMvc/Controllers/OpenIdController.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2009-03-16 09:22:08 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2009-03-16 09:22:08 -0700
commit45068758daa9d7747565af3f95bea7abfa2db54a (patch)
treeb11c17d32763b77e4bfc03005e202b0c14e8bbdf /samples/OpenIdProviderMvc/Controllers/OpenIdController.cs
parente72da4887ebf36073d1235e28e44b44004602311 (diff)
downloadDotNetOpenAuth-45068758daa9d7747565af3f95bea7abfa2db54a.zip
DotNetOpenAuth-45068758daa9d7747565af3f95bea7abfa2db54a.tar.gz
DotNetOpenAuth-45068758daa9d7747565af3f95bea7abfa2db54a.tar.bz2
Added ASP.NET MVC OpenID Provider sample.
Diffstat (limited to 'samples/OpenIdProviderMvc/Controllers/OpenIdController.cs')
-rw-r--r--samples/OpenIdProviderMvc/Controllers/OpenIdController.cs67
1 files changed, 67 insertions, 0 deletions
diff --git a/samples/OpenIdProviderMvc/Controllers/OpenIdController.cs b/samples/OpenIdProviderMvc/Controllers/OpenIdController.cs
new file mode 100644
index 0000000..f75377c
--- /dev/null
+++ b/samples/OpenIdProviderMvc/Controllers/OpenIdController.cs
@@ -0,0 +1,67 @@
+namespace OpenIdProviderMvc.Controllers {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Web;
+ using System.Web.Mvc;
+ using System.Web.Mvc.Ajax;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.OpenId.Provider;
+
+ public class OpenIdController : Controller {
+ internal static OpenIdProvider OpenIdProvider = new OpenIdProvider();
+
+ internal static IAuthenticationRequest PendingAuthenticationRequest {
+ get { return ProviderEndpoint.PendingAuthenticationRequest; }
+ set { ProviderEndpoint.PendingAuthenticationRequest = value; }
+ }
+
+ public ActionResult Provider() {
+ IRequest request = OpenIdProvider.GetRequest();
+ if (request != null) {
+ var authRequest = request as IAuthenticationRequest;
+ if (authRequest != null) {
+ PendingAuthenticationRequest = authRequest;
+ if (User.Identity.IsAuthenticated && (authRequest.IsDirectedIdentity || Models.User.GetClaimedIdentifierForUser(User.Identity.Name) == authRequest.LocalIdentifier)) {
+ return this.SendAssertion();
+ } else {
+ return RedirectToAction("LogOn", "Account", new { returnUrl = Url.Action("SendAssertion") });
+ }
+ }
+
+ if (request.IsResponseReady) {
+ return OpenIdProvider.PrepareResponse(request).AsActionResult();
+ } else {
+ return RedirectToAction("LogOn", "Account");
+ }
+ } else {
+ return View();
+ }
+ }
+
+ [Authorize]
+ public ActionResult SendAssertion() {
+ IAuthenticationRequest authReq = PendingAuthenticationRequest;
+ PendingAuthenticationRequest = null;
+ if (authReq == null) {
+ throw new InvalidOperationException();
+ }
+
+ if (authReq.IsDirectedIdentity) {
+ authReq.LocalIdentifier = Models.User.GetClaimedIdentifierForUser(User.Identity.Name);
+ authReq.ClaimedIdentifier = authReq.LocalIdentifier;
+ authReq.IsAuthenticated = true;
+ } else {
+ if (authReq.LocalIdentifier == Models.User.GetClaimedIdentifierForUser(User.Identity.Name)) {
+ authReq.IsAuthenticated = true;
+ if (!authReq.IsDelegatedIdentifier) {
+ authReq.ClaimedIdentifier = authReq.LocalIdentifier;
+ }
+ } else {
+ authReq.IsAuthenticated = false;
+ }
+ }
+ return OpenIdProvider.PrepareResponse(authReq).AsActionResult();
+ }
+ }
+}