diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2011-06-06 16:23:01 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2011-06-06 16:23:01 -0700 |
commit | 299fd439688e9e4a220c862b92ec82e82bdf0ab0 (patch) | |
tree | 52489fda9952d9aa7ccd59fab795e6862e24753b /samples/OAuthAuthorizationServer/Controllers/AccountController.cs | |
parent | e76823bc716477d3d5e26d17d0df7a2314bc2d82 (diff) | |
parent | dbbc823b7580d4e7d5251539a8dcace730df2e3f (diff) | |
download | DotNetOpenAuth-299fd439688e9e4a220c862b92ec82e82bdf0ab0.zip DotNetOpenAuth-299fd439688e9e4a220c862b92ec82e82bdf0ab0.tar.gz DotNetOpenAuth-299fd439688e9e4a220c862b92ec82e82bdf0ab0.tar.bz2 |
Merging OAuth 2.0 work into what will become DotNetOpenAuth 4.0.
Diffstat (limited to 'samples/OAuthAuthorizationServer/Controllers/AccountController.cs')
-rw-r--r-- | samples/OAuthAuthorizationServer/Controllers/AccountController.cs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/samples/OAuthAuthorizationServer/Controllers/AccountController.cs b/samples/OAuthAuthorizationServer/Controllers/AccountController.cs new file mode 100644 index 0000000..d69a3b5 --- /dev/null +++ b/samples/OAuthAuthorizationServer/Controllers/AccountController.cs @@ -0,0 +1,78 @@ +namespace OAuthAuthorizationServer.Controllers { + using System; + using System.Linq; + using System.Web.Mvc; + using System.Web.Security; + + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.RelyingParty; + + using OAuthAuthorizationServer.Code; + using OAuthAuthorizationServer.Models; + + [HandleError] + public class AccountController : Controller { + // ************************************** + // URL: /Account/LogOn + // ************************************** + public ActionResult LogOn() { + return View(); + } + + [HttpPost] + public ActionResult LogOn(LogOnModel model, string returnUrl) { + if (ModelState.IsValid) { + var rp = new OpenIdRelyingParty(); + var request = rp.CreateRequest(model.UserSuppliedIdentifier, Realm.AutoDetect, new Uri(Request.Url, Url.Action("Authenticate"))); + if (request != null) { + if (returnUrl != null) { + request.AddCallbackArguments("returnUrl", returnUrl); + } + + return request.RedirectingResponse.AsActionResult(); + } else { + ModelState.AddModelError(string.Empty, "The identifier you supplied is not recognized as a valid OpenID Identifier."); + } + } + + // If we got this far, something failed, redisplay form + return View(model); + } + + public ActionResult Authenticate(string returnUrl) { + var rp = new OpenIdRelyingParty(); + var response = rp.GetResponse(); + if (response != null) { + switch (response.Status) { + case AuthenticationStatus.Authenticated: + // Make sure we have a user account for this guy. + string identifier = response.ClaimedIdentifier; // convert to string so LinqToSQL expression parsing works. + if (MvcApplication.DataContext.Users.FirstOrDefault(u => u.OpenIDClaimedIdentifier == identifier) == null) { + MvcApplication.DataContext.Users.InsertOnSubmit(new User { + OpenIDFriendlyIdentifier = response.FriendlyIdentifierForDisplay, + OpenIDClaimedIdentifier = response.ClaimedIdentifier, + }); + } + + FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false); + return this.Redirect(returnUrl ?? Url.Action("Index", "Home")); + default: + ModelState.AddModelError(string.Empty, "An error occurred during login."); + break; + } + } + + return this.View("LogOn"); + } + + // ************************************** + // URL: /Account/LogOff + // ************************************** + public ActionResult LogOff() { + FormsAuthentication.SignOut(); + + return RedirectToAction("Index", "Home"); + } + } +} |