Skip to content
Browse files

Getting there

  • Loading branch information...
1 parent 7707c8d commit 405b45e8501cb99793846dcc6fc1ceb4c8ac198b @shanselman committed Sep 28, 2012
Showing with 161 additions and 133 deletions.
  1. +7 −6 .gitignore
  2. +53 −26 MVC4/PersonaMVC4Example/Controllers/PersonaController.cs
  3. +101 −101 MVC4/PersonaMVC4Example/Views/Account/Login.cshtml
View
13 .gitignore
@@ -1,7 +1,8 @@
-# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
-bin
-obj
-Packages
-
-# mstest test results
+# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
+bin
+obj
+Packages
+*.suo
+
+# mstest test results
TestResults
View
79 MVC4/PersonaMVC4Example/Controllers/PersonaController.cs
@@ -1,26 +1,53 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
-using System.Net.Http;
-using System.Web.Http;
-
-namespace PersonaMVC4Example.Controllers
-{
- [SimplePostVariableParameterBinding]
- public class PersonaController : ApiController
- {
- // POST api/persona
- [HttpPost][ActionName("login")]
- public void Login(string assertion)
- {
- var cookies = Request.Headers.GetCookies();
- string token = cookies[0]["__RequestVerificationToken"].Value;
- }
-
- [HttpPost][ActionName("logout")]
- public void Logout()
- {
- }
- }
-}
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Threading.Tasks;
+using System.Web;
+using System.Web.Http;
+
+namespace PersonaMVC4Example.Controllers
+{
+ [SimplePostVariableParameterBinding]
+ public class PersonaController : ApiController
+ {
+ // POST api/persona
+ [HttpPost][ActionName("login")]
+ public async Task<HttpResponseMessage> Login(string assertion)
+ {
+ if (assertion == null)
+ {
+ return new HttpResponseMessage(HttpStatusCode.BadRequest);
+ }
+ var cookies = Request.Headers.GetCookies();
+ string token = cookies[0]["__RequestVerificationToken"].Value;
+
+ using (var client = new HttpClient())
+ {
+ var content = new FormUrlEncodedContent(
+ new Dictionary<string, string> {
+ { "assertion", assertion },
+ { "audience", HttpContext.Current.Request.Url.Host }
+ //TODO: Can I get this without HttpContext.Current?
+ }
+ );
+ var result = await client.PostAsync("https://verifier.login.persona.org/verify", content);
+ var stringresult = await result.Content.ReadAsStringAsync();
+ dynamic jsonresult = JsonConvert.DeserializeObject<dynamic>(stringresult);
+ if (jsonresult.status == "okay")
+ {
+ return new HttpResponseMessage(HttpStatusCode.OK);
+ }
+ }
+ return new HttpResponseMessage(HttpStatusCode.Forbidden);
+ }
+
+ [HttpPost][ActionName("logout")]
+ public void Logout()
+ {
+ }
+ }
+}
View
202 MVC4/PersonaMVC4Example/Views/Account/Login.cshtml
@@ -1,101 +1,101 @@
-@model PersonaMVC4Example.Models.LoginModel
-
-@{
- ViewBag.Title = "Log in";
-}
-
-<hgroup class="title">
- <h1>@ViewBag.Title.</h1>
-</hgroup>
-
-<section id="loginForm">
-<h2>Use a local account to log in.</h2>
-@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
- @Html.AntiForgeryToken()
- @Html.ValidationSummary(true)
-
- <fieldset>
- <legend>Log in Form</legend>
- <ol>
- <li>
- @Html.LabelFor(m => m.UserName)
- @Html.TextBoxFor(m => m.UserName)
- @Html.ValidationMessageFor(m => m.UserName)
- </li>
- <li>
- @Html.LabelFor(m => m.Password)
- @Html.PasswordFor(m => m.Password)
- @Html.ValidationMessageFor(m => m.Password)
- </li>
- <li>
- @Html.CheckBoxFor(m => m.RememberMe)
- @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
- </li>
- </ol>
- <input type="submit" value="Log in" />
- </fieldset>
- <p>
- @Html.ActionLink("Register", "Register") if you don't have an account.
- </p>
-}
-</section>
-
-<section class="social" id="socialLoginForm">
- <h2>Use another service to log in.</h2>
- @Html.Action("ExternalLoginsList", new { ReturnUrl = ViewBag.ReturnUrl })
-
- <h2>Mozilla Persona (Example)</h2>
- <p><a href="#" class="persona-button" id="personasignin"><span>Sign in with your Email</span></a></p>
- <p><a href="#" class="persona-button" id="personasignout"><span>Sign out</span></a></p>
- <!-- The CSS for this is in persona-buttons.css and is bundled in in BundleConfig.cs -->
-</section>
-
-@section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
-
- <!--
- Script from https://developer.mozilla.org/en-US/docs/Persona/Quick_Setup
- Should be moved into its own JavaScript
- -->
- <script>
- var signinLink = document.getElementById('personasignin');
- if (signinLink) {
- signinLink.onclick = function () { navigator.id.request(); };
- };
-
- var signoutLink = document.getElementById('personasignout');
- if (signoutLink) {
- signoutLink.onclick = function () { navigator.id.logout(); };
- };
-
- var currentUser = 'scott@hanselman.com';
-
- navigator.id.watch({
- loggedInUser: currentUser,
- onlogin: function (assertion) {
- // A user has logged in! Here you need to:
- // 1. Send the assertion to your backend for verification and to create a session.
- // 2. Update your UI.
- $.ajax({ /* <-- This example uses jQuery, but you can use whatever you'd like */
- type: 'POST',
- url: '/api/persona/login', // This is a URL on your website.
- data: { assertion: assertion },
- success: function (res, status, xhr) { window.location.reload(); },
- error: function (res, status, xhr) { alert("login failure" + res); }
- });
- },
- onlogout: function () {
- // A user has logged out! Here you need to:
- // Tear down the user's session by redirecting the user or making a call to your backend.
- $.ajax({
- type: 'POST',
- url: '/api/persona/logout', // This is a URL on your website.
- success: function (res, status, xhr) { /* window.location.reload(); */ },
- error: function (res, status, xhr) { alert("logout failure" + res); }
- });
- }
- });
-
-
- </script>
-}
+@model PersonaMVC4Example.Models.LoginModel
+
+@{
+ ViewBag.Title = "Log in";
+}
+
+<hgroup class="title">
+ <h1>@ViewBag.Title.</h1>
+</hgroup>
+
+<section id="loginForm">
+<h2>Use a local account to log in.</h2>
+@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl })) {
+ @Html.AntiForgeryToken()
+ @Html.ValidationSummary(true)
+
+ <fieldset>
+ <legend>Log in Form</legend>
+ <ol>
+ <li>
+ @Html.LabelFor(m => m.UserName)
+ @Html.TextBoxFor(m => m.UserName)
+ @Html.ValidationMessageFor(m => m.UserName)
+ </li>
+ <li>
+ @Html.LabelFor(m => m.Password)
+ @Html.PasswordFor(m => m.Password)
+ @Html.ValidationMessageFor(m => m.Password)
+ </li>
+ <li>
+ @Html.CheckBoxFor(m => m.RememberMe)
+ @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
+ </li>
+ </ol>
+ <input type="submit" value="Log in" />
+ </fieldset>
+ <p>
+ @Html.ActionLink("Register", "Register") if you don't have an account.
+ </p>
+}
+</section>
+
+<section class="social" id="socialLoginForm">
+ <h2>Use another service to log in.</h2>
+ @Html.Action("ExternalLoginsList", new { ReturnUrl = ViewBag.ReturnUrl })
+
+ <h2>Mozilla Persona (Example)</h2>
+ <p><a href="#" class="persona-button" id="personasignin"><span>Sign in with your Email</span></a></p>
+ <p><a href="#" class="persona-button" id="personasignout"><span>Sign out</span></a></p>
+ <!-- The CSS for this is in persona-buttons.css and is bundled in in BundleConfig.cs -->
+</section>
+
+@section Scripts {
+ @Scripts.Render("~/bundles/jqueryval")
+
+ <!--
+ Script from https://developer.mozilla.org/en-US/docs/Persona/Quick_Setup
+ Should be moved into its own JavaScript
+ -->
+ <script>
+ var signinLink = document.getElementById('personasignin');
+ if (signinLink) {
+ signinLink.onclick = function () { navigator.id.request(); };
+ };
+
+ var signoutLink = document.getElementById('personasignout');
+ if (signoutLink) {
+ signoutLink.onclick = function () { navigator.id.logout(); };
+ };
+
+ var currentUser = "scott@hanselman.com"; /* or email */
+
+ navigator.id.watch({
+ loggedInUser: currentUser,
+ onlogin: function (assertion) {
+ // A user has logged in! Here you need to:
+ // 1. Send the assertion to your backend for verification and to create a session.
+ // 2. Update your UI.
+ $.ajax({ /* <-- This example uses jQuery, but you can use whatever you'd like */
+ type: 'POST',
+ url: '/api/persona/login', // This is a URL on your website.
+ data: { assertion: assertion },
+ success: function (res, status, xhr) { window.location.reload(); },
+ error: function (res, status, xhr) { alert("login failure" + res); }
+ });
+ },
+ onlogout: function () {
+ // A user has logged out! Here you need to:
+ // Tear down the user's session by redirecting the user or making a call to your backend.
+ $.ajax({
+ type: 'POST',
+ url: '/api/persona/logout', // This is a URL on your website.
+ success: function (res, status, xhr) { window.location.reload(); },
+ error: function (res, status, xhr) { alert("logout failure" + res); }
+ });
+ }
+ });
+
+
+ </script>
+}

0 comments on commit 405b45e

Please sign in to comment.
Something went wrong with that request. Please try again.