summaryrefslogtreecommitdiffstats
path: root/samples/OpenIdProviderMvc/Controllers/OpenIdController.cs
diff options
context:
space:
mode:
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();
+ }
+ }
+}