/
PersonaController.cs
87 lines (79 loc) · 3.16 KB
/
PersonaController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Microsoft.Web.WebPages.OAuth;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PersonaMVC4Example.Models;
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;
using System.Web.Security;
using WebMatrix.WebData;
namespace PersonaMVC4Example.Controllers
{
[HttpHeaderAntiForgeryTokenAttribute]
public class PersonaController : ApiController
{
// POST api/persona
[HttpPost][ActionName("login")]
public async Task<HttpResponseMessage> Login([FromBody] string assertion)
{
if (assertion == null)
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
using (var client = new HttpClient())
{
var content = new FormUrlEncodedContent(
new Dictionary<string, string> {
{ "assertion", assertion },
{ "audience", HttpContext.Current.Request.Url.Host },
}
);
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")
{
string email = jsonresult.email;
string userName = null;
if (User.Identity.IsAuthenticated)
{
userName = User.Identity.Name;
}
else
{
userName = OAuthWebSecurity.GetUserName("Persona", email);
if (userName == null)
{
userName = email; // TODO: prompt for user name
using (UsersContext db = new UsersContext())
{
UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == userName.ToLower());
// Check if user already exists
if (user == null)
{
// Insert name into the profile table
db.UserProfiles.Add(new UserProfile { UserName = userName });
db.SaveChanges();
}
}
}
}
OAuthWebSecurity.CreateOrUpdateAccount("Persona", email, userName);
FormsAuthentication.SetAuthCookie(email, false);
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
return new HttpResponseMessage(HttpStatusCode.Forbidden);
}
[HttpPost][ActionName("logout")]
public void Logout()
{
WebSecurity.Logout();
}
}
}