summaryrefslogtreecommitdiffstats
path: root/src/OAuth/OAuthAuthorizationServer/Controllers/AccountController.cs
diff options
context:
space:
mode:
authorDavid Christiansen <coding@davedoes.net>2012-03-15 22:10:55 +0000
committerDavid Christiansen <coding@davedoes.net>2012-03-15 22:10:55 +0000
commita5bfa2bb8a614b1932ec8b7bbc6a0cc6bca3051f (patch)
treea3057157fa3287e0c0c4cc49be1854f9aa63d321 /src/OAuth/OAuthAuthorizationServer/Controllers/AccountController.cs
parent02ce959db12fec57e846e5ebfa662cd0327ce69c (diff)
downloadDotNetOpenAuth.Samples-a5bfa2bb8a614b1932ec8b7bbc6a0cc6bca3051f.zip
DotNetOpenAuth.Samples-a5bfa2bb8a614b1932ec8b7bbc6a0cc6bca3051f.tar.gz
DotNetOpenAuth.Samples-a5bfa2bb8a614b1932ec8b7bbc6a0cc6bca3051f.tar.bz2
W.I.P.
* Initial migration and reference to DNOA Nuget packages (From teamcity.dotnetopenauth.net) * Awaiting fix to DotNetOpenAuth.OpenIdOAuth.nuspec in order to complete migration.
Diffstat (limited to 'src/OAuth/OAuthAuthorizationServer/Controllers/AccountController.cs')
-rw-r--r--src/OAuth/OAuthAuthorizationServer/Controllers/AccountController.cs78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/OAuth/OAuthAuthorizationServer/Controllers/AccountController.cs b/src/OAuth/OAuthAuthorizationServer/Controllers/AccountController.cs
new file mode 100644
index 0000000..d69a3b5
--- /dev/null
+++ b/src/OAuth/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");
+ }
+ }
+}