Created
June 18, 2016 19:05
-
-
Save omkarkhair/de1e0c478c7f0306242d7927ac60a747 to your computer and use it in GitHub Desktop.
OWIN Open ID Connect
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 Microsoft.IdentityModel.Clients.ActiveDirectory; | |
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace PUGWebApp.Controllers | |
{ | |
public class SetupController : Controller | |
{ | |
// GET: Setup | |
public ActionResult Index() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public ActionResult Index() | |
{ | |
// generate a random value to identify the request | |
string stateMarker = Guid.NewGuid().ToString(); | |
string authorizationRequest = String.Format( | |
"https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id={0}&resource={1}&redirect_uri={2}&state={3}", | |
Uri.EscapeDataString(ConfigurationManager.AppSettings["ida:ClientID"]), | |
Uri.EscapeDataString("https://graph.windows.net"), | |
Uri.EscapeDataString(this.Request.Url.GetLeftPart(UriPartial.Authority).ToString() + "/Setup/Process"), | |
Uri.EscapeDataString(stateMarker) | |
); | |
// Add admin consent prompt | |
authorizationRequest += String.Format("&prompt={0}", Uri.EscapeDataString("admin_consent")); | |
// send the user to consent | |
return new RedirectResult(authorizationRequest); | |
} | |
public async Task<ActionResult> Process(string code, string error, string error_description, string resource, string state) | |
{ | |
ClientCredential credential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientID"], | |
ConfigurationManager.AppSettings["ida:Password"]); | |
AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common/"); | |
AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync( | |
code, new Uri(Request.Url.GetLeftPart(UriPartial.Path)), credential); | |
return RedirectToAction("Profile","Home"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment