public string GetAccessToken() { ApplicationDbContext db = new ApplicationDbContext(); string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"]; string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; string tenantId = ConfigurationManager.AppSettings["ida:TenantId"]; string Authority = aadInstance + tenantId; string claimIdName = ClaimTypes.NameIdentifier; string claimIdTenantId = "http://schemas.microsoft.com/identity/claims/tenantid"; string claimIdUserId = "http://schemas.microsoft.com/identity/claims/objectidentifier"; ClaimsPrincipal currentUserClaims = ClaimsPrincipal.Current; string signedInUserID = currentUserClaims.FindFirst(claimIdName).Value; string tenantID = currentUserClaims.FindFirst(claimIdTenantId).Value; string userObjectID = currentUserClaims.FindFirst(claimIdUserId).Value; // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc) ClientCredential clientcred = new ClientCredential(clientId, appKey); // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID)); AuthenticationResult authenticationResult = authenticationContext.AcquireTokenSilentAsync(resource, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)).Result; return authenticationResult.AccessToken; }
public void ConfigureAuth(IAppBuilder app) { ApplicationDbContext db = new ApplicationDbContext(); app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { ClientId = clientId, Authority = Authority, PostLogoutRedirectUri = postLogoutRedirectUri, Notifications = new OpenIdConnectAuthenticationNotifications() { // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away. AuthorizationCodeReceived = (context) => { var code = context.Code; ClientCredential credential = new ClientCredential(clientId, appKey); string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID)); AuthenticationResult result = authContext.AcquireTokenByAuthorizationCodeAsync( code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, resourceAzureGraphAPI).Result; return Task.FromResult(0); } } }); }
public static string GetAccessToken(string resource) { // get ClaimsPrincipal for current user ClaimsPrincipal currentUserClaims = ClaimsPrincipal.Current; string signedInUserID = currentUserClaims.FindFirst(ClaimTypes.NameIdentifier).Value; string tenantID = currentUserClaims.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; string userObjectID = currentUserClaims.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; ApplicationDbContext db = new ApplicationDbContext(); ADALTokenCache userTokenCache = new ADALTokenCache(signedInUserID); string urlAuthorityRoot = ConfigurationManager.AppSettings["ida:AADInstance"]; string urlAuthorityTenant = urlAuthorityRoot + tenantID; AuthenticationContext authenticationContext = new AuthenticationContext(urlAuthorityTenant, userTokenCache); Uri uriReplyUrl = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)); string clientId = ConfigurationManager.AppSettings["ida:ClientId"]; string clientSecret = ConfigurationManager.AppSettings["ida:ClientSecret"]; ClientCredential clientCredential = new ClientCredential(clientId, clientSecret); UserIdentifier userIdentifier = new UserIdentifier(userObjectID, UserIdentifierType.UniqueId); AuthenticationResult authenticationResult = authenticationContext.AcquireTokenSilentAsync(resource, clientCredential, userIdentifier).Result; return authenticationResult.AccessToken; }
public static ADALTokenCache GetTokenCache() { // get ClaimsPrincipal for current user ClaimsPrincipal currentUserClaims = ClaimsPrincipal.Current; string signedInUserID = currentUserClaims.FindFirst(ClaimTypes.NameIdentifier).Value; string userObjectID = currentUserClaims.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; ApplicationDbContext db = new ApplicationDbContext(); ADALTokenCache userTokenCache = new ADALTokenCache(signedInUserID); return userTokenCache; }