Created
April 10, 2017 01:13
-
-
Save jakkaj/7cdfa3a44bda4e7b2156c83d18e17fc8 to your computer and use it in GitHub Desktop.
Ignite Australia Bot Authenication Controller
This file contains hidden or 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 EventBot.Ignite; | |
using Microsoft.ApplicationInsights; | |
using Microsoft.Bot.Builder.Dialogs; | |
using Microsoft.Bot.Connector; | |
using Microsoft.IdentityModel.Protocols; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Configuration; | |
using System.Diagnostics; | |
using System.IdentityModel.Services; | |
using System.IdentityModel.Tokens; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Security.Claims; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.Http; | |
using System.Xml; | |
using System.Web.Http.Results; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Autofac; | |
using DotNetOpenAuth.OAuth2; | |
using EventBot.SupportLibrary.Contract; | |
using Xamling.Azure.Portable.Contract; | |
using XamlingCore.Portable.Data.Glue; | |
namespace EventBot.Controllers | |
{ | |
public class AuthenticationController : Controller | |
{ | |
private WebServerClient _webServerClient; | |
private Uri _authorizationServerUri; | |
class Paths | |
{ | |
public const string AuthorizePath = "/OAuth/Authorize"; | |
public const string TokenPath = "/OAuth/Token"; | |
public const string LoginPath = "/Account/Login"; | |
public const string LogoutPath = "/Account/Logout"; | |
} | |
void _setup() | |
{ | |
_authorizationServerUri = new Uri(ConfigurationManager.AppSettings["AuthServerBaseUrl"]); | |
var authorizationServer = new AuthorizationServerDescription | |
{ | |
AuthorizationEndpoint = new Uri(_authorizationServerUri, Paths.AuthorizePath), | |
TokenEndpoint = new Uri(_authorizationServerUri, Paths.TokenPath) | |
}; | |
_webServerClient = new WebServerClient(authorizationServer, ConfigurationManager.AppSettings["AuthServerClientId"], ConfigurationManager.AppSettings["AuthServerClientKey"]); | |
} | |
public async Task<ActionResult> Index() | |
{ | |
_setup(); | |
var tc = ContainerHost.Container.Resolve<ILogService>(); | |
var guid = Guid.NewGuid(); | |
var telemetryDict = new Dictionary<string, string> | |
{ | |
{"telemetryGuid", guid.ToString()} | |
}; | |
var rvError = new RouteValueDictionary(); | |
rvError.Add("telemetryGuid", guid); | |
var queryString = new Dictionary<string,string>(); | |
foreach (var k in Request.QueryString.AllKeys) | |
{ | |
queryString.Add(k, Request.QueryString[k]); | |
} | |
if (!queryString.ContainsKey("rc")) | |
{ | |
//redirect to some other page. | |
tc.TrackTrace("No RC returned", telemetryDict); | |
return RedirectToAction("NotLoggedIn", "LoginResult", rvError); | |
} | |
if (!queryString.ContainsKey("code")) | |
{ | |
var userAuthorization = _webServerClient.PrepareRequestUserAuthorization(new[] {"MicrosoftIgnite"}, | |
new Uri(Request.Url.ToString())); | |
userAuthorization.Send(HttpContext); | |
Response.End(); | |
} | |
else | |
{ | |
var authorizationState = _webServerClient.ProcessUserAuthorization(this.Request); | |
if (authorizationState != null) | |
{ | |
var accessToken = authorizationState.AccessToken; | |
var rc = queryString["rc"]; | |
var loginService = ContainerHost.Container.Resolve<ILoginHandlerService>(); | |
var result = await loginService.HandleLoginReturned(rc, accessToken); | |
if (result) | |
{ | |
return RedirectToAction("LoggedIn", "LoginResult"); | |
} | |
else | |
{ | |
tc.TrackTrace("Login Failed", telemetryDict); | |
return RedirectToAction("NotLoggedIn", "LoginResult", rvError); | |
} | |
} | |
} | |
return View(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment