Last active
August 29, 2015 14:08
-
-
Save estruyf/b7c7603d33c902860e9f to your computer and use it in GitHub Desktop.
MVC ACS Redirection (OWA and Rich Client)
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 System; | |
using System.Configuration; | |
using System.Threading.Tasks; | |
using System.Web.Mvc; | |
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
namespace Demo_DemoFinal_MVC5.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
private readonly string _clientId = ConfigurationManager.AppSettings["ida:ClientID"]; | |
private readonly string _appKey = ConfigurationManager.AppSettings["ida:Password"]; | |
private readonly string _authorizationUri = ConfigurationManager.AppSettings["ida:AuthorizationUri"]; | |
private const string ServiceResourceId = "https://<tenant>.sharepoint.com"; | |
public async Task<ActionResult> Index(string et, string code, string error, string error_description) | |
{ | |
if (error != null) return RedirectToAction("Error", new {error, error_description}); | |
var authContext = new AuthenticationContext(_authorizationUri + "/common", true); | |
// Check if you received an authorization code, if not you need to sign in | |
if (code == null) | |
{ | |
// Get the authorization URL (https://login.windows.net/common/oauth2/authorize) | |
var redirectUri = authContext.GetAuthorizationRequestURL(ServiceResourceId, _clientId, new Uri(Request.Url.AbsoluteUri.Split('?')[0]), UserIdentifier.AnyUser, string.Empty); | |
// Redirect the client based on the context (Rich Client or Online) | |
if (et == null) | |
{ | |
return Redirect(redirectUri.ToString()); | |
} | |
@ViewBag.URL = redirectUri.ToString(); | |
return View(); | |
} | |
// If the code is empty, show the user the error page | |
if (String.IsNullOrEmpty(code)) return RedirectToAction("Error", new { error = "AuthToken", error_description = "The authorization token was not retrieved."}); | |
// If a code is retrieved, the access token can be generated | |
var clientCredentials = new ClientCredential(_clientId, _appKey); | |
var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(code, new Uri(Request.Url.AbsoluteUri.Split('?')[0]), clientCredentials); | |
Session["AccessToken"] = authResult.AccessToken; | |
return RedirectToAction("SiteInfo"); | |
} | |
public ActionResult SiteInfo() | |
{ | |
var accessToken = Session["AccessToken"].ToString(); | |
if (!string.IsNullOrEmpty(accessToken)) | |
{ | |
using(var clientContext = TokenHelper.GetClientContextWithAccessToken(ServiceResourceId, accessToken)) { | |
var web = clientContext.Web; | |
clientContext.Load(web, w => w.Title, w => w.CurrentUser.Title); | |
clientContext.ExecuteQuery(); | |
ViewBag.SiteTitle = web.Title; | |
ViewBag.User = web.CurrentUser.Title; | |
} | |
} | |
return View(); | |
} | |
public ActionResult Error(string error, string error_description) | |
{ | |
ViewBag.Error = error; | |
ViewBag.Desc = error_description; | |
return View(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment