Last active
October 20, 2016 22:26
-
-
Save BMeyerKC/ed0e57d37ad612f1693d8e3700c8f4e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using RealityLeague.Attributes; | |
using RestSharp; | |
using RLLib.Facade; | |
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace RealityLeague.Controllers | |
{ | |
public class AuthController : BaseController | |
{ | |
/* | |
<add key="fbclientid" value="facebookclientid" /> | |
<add key="fbsecret" value="facebooksecret" /> | |
<add key="fbrequesturi" value="http://local.com/auth/facebook" /> | |
<add key="gaclientid" value="googleclientid" /> | |
<add key="gasecret" value="googlesecret" /> | |
<add key="garequesturi" value="http://local.com/auth/google" /> | |
*/ | |
private class OAuthTokenResponse | |
{ | |
public string access_token { get; set; } | |
public string token_type { get; set; } | |
public int expires_in { get; set; } | |
} | |
private class FBMe | |
{ | |
public string id { get; set; } | |
public string email { get; set; } | |
public string name { get; set; } | |
} | |
public ActionResult Facebook() | |
{ | |
//do something with the auth. | |
var client = new RestClient("https://graph.facebook.com/v2.3/"); | |
var authRequest = new RestRequest("oauth/access_token", Method.GET); | |
authRequest.AddParameter("client_id", ConfigurationManager.AppSettings["fbclientid"]); | |
authRequest.AddParameter("redirect_uri", ConfigurationManager.AppSettings["fbrequesturi"]); | |
authRequest.AddParameter("client_secret", ConfigurationManager.AppSettings["fbsecret"]); | |
authRequest.AddParameter("code", HttpContext.Request.QueryString["code"]); | |
IRestResponse<OAuthTokenResponse> oauthToken = client.Execute<OAuthTokenResponse>(authRequest); | |
if (oauthToken.Data.access_token == null) Response.Redirect("/Account/Logon"); | |
var meRequest = new RestRequest("me", Method.GET); | |
meRequest.AddParameter("access_token", oauthToken.Data.access_token); | |
IRestResponse<FBMe> me = client.Execute<FBMe>(meRequest); | |
LogonWithOauth(me.Data.email, me.Data.name, oauthToken.Data.access_token); | |
return RedirectToAction("Index", "Account"); | |
} | |
private class GAMe | |
{ | |
public string id { get; set; } | |
public string displayName { get; set; } | |
public List<gEmails> emails { get; set; } | |
public class gEmails | |
{ | |
public string value { get; set; } | |
public string type { get; set; } | |
} | |
} | |
public ActionResult Google() | |
{ | |
//do something with the auth. | |
var client = new RestClient("https://www.googleapis.com/"); | |
var authRequest = new RestRequest("oauth2/v3/token", Method.POST); | |
authRequest.AddParameter("client_id", ConfigurationManager.AppSettings["gaclientid"]); | |
authRequest.AddParameter("redirect_uri", ConfigurationManager.AppSettings["garequesturi"]); | |
authRequest.AddParameter("client_secret", ConfigurationManager.AppSettings["gasecret"]); | |
authRequest.AddParameter("code", HttpContext.Request.QueryString["code"]); | |
authRequest.AddParameter("grant_type", "authorization_code"); | |
IRestResponse<OAuthTokenResponse> oauthToken = client.Execute<OAuthTokenResponse>(authRequest); | |
if (oauthToken.Data.access_token == null) Response.Redirect("/Account/Logon"); | |
var meRequest = new RestRequest("plus/v1/people/me", Method.GET); | |
meRequest.AddParameter("access_token", oauthToken.Data.access_token); | |
IRestResponse<GAMe> me = client.Execute<GAMe>(meRequest); | |
LogonWithOauth(me.Data.emails[0].value, me.Data.displayName, oauthToken.Data.access_token); | |
return RedirectToAction("Index", "Account"); | |
} | |
private void LogonWithOauth(string email, string name, string token) | |
{ | |
var accountFacade = new AccountFacade(Conn); | |
var logonToken = accountFacade.accountLogonTokenByEmailAndOauthtoken(email, name, token); | |
var userCookie = new System.Web.HttpCookie("userToken") | |
{ | |
Value = logonToken, | |
HttpOnly = true, | |
Expires = DateTime.Now.AddMonths(1) | |
}; | |
Response.Cookies.Add(rltvCookie); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment