This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jim Crowley
committed
Mar 6, 2018
1 parent
c9da9b6
commit 26bdbce
Showing
14 changed files
with
258 additions
and
213 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. | ||
* See LICENSE in the source repository root for complete license information. | ||
*/ | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft_Graph_ExcelRest_ToDo.Helpers | ||
{ | ||
public interface IAuthProvider | ||
{ | ||
Task<string> GetUserAccessTokenAsync(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
Microsoft-Graph-ExcelRest-ToDo/Helpers/SampleAuthProvider.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. | ||
* See LICENSE in the source repository root for complete license information. | ||
*/ | ||
|
||
using Microsoft.Identity.Client; | ||
using Microsoft.Owin.Security; | ||
using Microsoft.Owin.Security.OpenIdConnect; | ||
using Microsoft_Graph_ExcelRest_ToDo.TokenStorage; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using System; | ||
|
||
namespace Microsoft_Graph_ExcelRest_ToDo.Helpers | ||
{ | ||
public sealed class SampleAuthProvider : IAuthProvider | ||
{ | ||
|
||
// Properties used to get and manage an access token. | ||
private string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"]; | ||
private string appId = ConfigurationManager.AppSettings["ida:AppId"]; | ||
private string appSecret = ConfigurationManager.AppSettings["ida:AppSecret"]; | ||
private string scopes = ConfigurationManager.AppSettings["ida:GraphScopes"]; | ||
private SessionTokenCache tokenCache { get; set; } | ||
|
||
private static readonly SampleAuthProvider instance = new SampleAuthProvider(); | ||
private SampleAuthProvider() { } | ||
|
||
public static SampleAuthProvider Instance | ||
{ | ||
get | ||
{ | ||
return instance; | ||
} | ||
} | ||
|
||
// Gets an access token. First tries to get the token from the token cache. | ||
public async Task<string> GetUserAccessTokenAsync() | ||
{ | ||
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; | ||
HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current); | ||
TokenCache userTokenCache = new SessionTokenCache(signedInUserID, httpContext).GetMsalCacheInstance(); | ||
//var cachedItems = tokenCache.ReadItems(appId); // see what's in the cache | ||
|
||
ConfidentialClientApplication cca = new ConfidentialClientApplication( | ||
appId, | ||
redirectUri, | ||
new ClientCredential(appSecret), | ||
userTokenCache, | ||
null); | ||
|
||
try | ||
{ | ||
AuthenticationResult result = await cca.AcquireTokenSilentAsync(scopes.Split(new char[] { ' ' }), cca.Users.First()); | ||
return result.AccessToken; | ||
} | ||
|
||
// Unable to retrieve the access token silently. | ||
catch (Exception) | ||
{ | ||
HttpContext.Current.Request.GetOwinContext().Authentication.Challenge( | ||
new AuthenticationProperties() { RedirectUri = "/" }, | ||
OpenIdConnectAuthenticationDefaults.AuthenticationType); | ||
|
||
throw new Exception("Caller needs to authenticate."); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.