private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification) { notification.HandleCodeRedemption(); IConfidentialClientApplication confidentialClient = MsalAppBuilder .BuildConfidentialClientApplication(new ClaimsPrincipal(notification.AuthenticationTicket.Identity)); AuthenticationResult result = await confidentialClient .AcquireTokenByAuthorizationCode(Globals.Scopes, notification.Code).ExecuteAsync(); notification.HandleCodeRedemption(null, result.IdToken); }
private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification) { notification.HandleCodeRedemption(); var idClient = ConfidentialClientApplicationBuilder.Create(appId) .WithRedirectUri(redirectUri) .WithClientSecret(appSecret) .Build(); var signedInUser = new ClaimsPrincipal(notification.AuthenticationTicket.Identity); var tokenStore = new SessionTokenStore(idClient.UserTokenCache, HttpContext.Current, signedInUser); try { string[] scopes = graphScopes.Split(' '); var result = await idClient.AcquireTokenByAuthorizationCode( scopes, notification.Code).ExecuteAsync(); var userDetails = await GraphHelper.GetUserDetailsAsync(result.AccessToken); var handler = new JwtSecurityTokenHandler(); var jsonToken = handler.ReadToken(result.IdToken) as JwtSecurityToken; var sid = jsonToken.Claims.First(claim => claim.Type == "sid"); tokenStore.SaveSid(sid.Value); tokenStore.SaveUserDetails(userDetails); notification.HandleCodeRedemption(null, result.IdToken); } catch (MsalException ex) { string message = "AcquireTokenByAuthorizationCodeAsync threw an exception"; notification.HandleResponse(); notification.Response.Redirect($"/Home/Error?message={message}&debug={ex.Message}"); } catch (Microsoft.Graph.ServiceException ex) { string message = "GetUserDetailsAsync threw an exception"; notification.HandleResponse(); notification.Response.Redirect($"/Home/Error?message={message}&debug={ex.Message}"); } }
private static async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification) { //IConfidentialClientApplication clientApp = MsalAppBuilder. notification.HandleCodeRedemption(); var idClient = ConfidentialClientApplicationBuilder.Create(appId) .WithRedirectUri(redirectUri) .WithClientSecret(appSecret) .Build(); var signedInUser = new ClaimsPrincipal(notification.AuthenticationTicket.Identity); var tokenStore = new SessionTokenStore(idClient.UserTokenCache, HttpContext.Current, signedInUser); try { string[] scopes = graphScopes.Split(' '); var result = await idClient.AcquireTokenByAuthorizationCode( scopes, notification.Code).ExecuteAsync(); //var userMessage = await GraphHelper.GetMeAsync(result.AccessToken); //var userSend = await GraphHelper.SendMailAsync(result.AccessToken); //var userDetails = await OutlookFW.Web.Controllers.MailController._mailAppService.GetUserDetailsAsync(result.AccessToken); //email= userDetails.Email.ToString(); accessToken = result.AccessToken; //tokenStore.SaveUserDetails(userDetails); notification.HandleCodeRedemption(null, result.IdToken); } catch (MsalException ex) { string message = "AcquireTokenByAuthorizationCodeAsync threw an exception"; notification.HandleResponse(); notification.Response.Redirect($"/Home/Error?message={message}&debug={ex.Message}"); } catch (Microsoft.Graph.ServiceException ex) { string message = "GetUserDetailsAsync threw an exception"; notification.HandleResponse(); notification.Response.Redirect($"/Home/Error?message={message}&debug={ex.Message}"); } }
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context) { context.TokenEndpointRequest.Parameters.TryGetValue("code_verifier", out var codeVerifier); // Upon successful sign in, get the access token & cache it using MSAL IConfidentialClientApplication clientApp = MsalAppBuilder.BuildConfidentialClientApplication(); AuthenticationResult result = await clientApp.AcquireTokenByAuthorizationCode(new[] { "Mail.Read User.Read" }, context.Code) .WithSpaAuthorizationCode() //Request an authcode for the front end .WithPkceCodeVerifier(codeVerifier) // Code verifier for PKCE .ExecuteAsync(); HttpContext.Current.Session.Add("Spa_Auth_Code", result.SpaAuthCode); // This continues the authentication flow using the access token and id token retrieved by the clientApp object after // redeeming an access token using the access code. // // This is needed to ensure the middleware does not try and redeem the received access code a second time. context.HandleCodeRedemption(result.AccessToken, result.IdToken); }