public ActionResult SignOut() { if (Request.IsAuthenticated) { var tokenStore = new SessionTokenStorage.SessionTokenStore(null, System.Web.HttpContext.Current, ClaimsPrincipal.Current); tokenStore.Clear(); Request.GetOwinContext().Authentication.SignOut( CookieAuthenticationDefaults.AuthenticationType); } return(RedirectToAction("Index", "Home")); }
private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification) { var idClient = ConfidentialClientApplicationBuilder.Create(appId) .WithRedirectUri(redirectUri) .WithClientSecret(appSecret) .Build(); var signedInUser = new ClaimsPrincipal(notification.AuthenticationTicket.Identity); var tokenStore = new SessionTokenStorage.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 cachedUser = new SessionTokenStorage.CachedUser() { DisplayName = userDetails.DisplayName, Email = string.IsNullOrEmpty(userDetails.Mail) ? userDetails.UserPrincipalName : userDetails.Mail, Avatar = string.Empty }; tokenStore.SaveUserDetails(cachedUser); } 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}"); } }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (Request.IsAuthenticated) { var tokenStore = new SessionTokenStorage.SessionTokenStore(null, System.Web.HttpContext.Current, ClaimsPrincipal.Current); if (tokenStore.HasData()) { ViewBag.User = tokenStore.GetUserDetails(); } else { // The session has lost data. This happens often // when debugging. Log out so the user can log back in Request.GetOwinContext().Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType); filterContext.Result = RedirectToAction("Index", "Home"); } } base.OnActionExecuting(filterContext); }
internal static GraphServiceClient GetAuthenticatedClient() { return(new GraphServiceClient( new DelegateAuthenticationProvider( async(requestMessage) => { var idClient = ConfidentialClientApplicationBuilder.Create(appId) .WithRedirectUri(redirectUri) .WithClientSecret(appSecret) .Build(); var tokenStore = new SessionTokenStorage.SessionTokenStore(idClient.UserTokenCache, HttpContext.Current, ClaimsPrincipal.Current); var accounts = await idClient.GetAccountsAsync(); var scopes = graphScopes.Split(' '); var result = await idClient.AcquireTokenSilent(scopes, accounts.FirstOrDefault()) .ExecuteAsync(); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); }))); }