//Working AddADAccountClick(ASYNC) // public async Task<ActionResult> AddADAccountClick(string searchtext) // { // //string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value; // //string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; // try // { // // Get a token for the Microsoft Graph // //string token = await GetGraphAccessToken(userId, new string[] { "user.readbasic.all" }); // string token = Utility.GetAccessToken(); // // Construct the query // HttpClient client = new HttpClient(); // //Using Mail ID. // //HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/users?$filter=startswith(givenName,'" + searchtext + "' )"); // //Using Name. // HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,'" + searchtext + "')"); // request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); // // Ensure a successful response // HttpResponseMessage response = await client.SendAsync(request); // response.EnsureSuccessStatusCode(); // // Populate the data store with the first page of groups // string json = await response.Content.ReadAsStringAsync(); // UserResponse result = JsonConvert.DeserializeObject<UserResponse>(json); // ViewBag.TenantId = result; // } // // If the tokens have expired or become invalid for any reason, ask the user to sign in again // catch (MsalUiRequiredException ex) // { // return new RedirectResult("/Account/SignIn"); // } // // Handle unexpected errors. // catch (Exception ex) // { // } // //ViewBag.TenantId = tenantId; // // return View(); // return Json(ViewBag.TenantId, JsonRequestBehavior.AllowGet); //} public ActionResult AddADAccountClick(string searchtext) { try { var user = Utility.AddADAccountNameClickAsync(searchtext); ViewBag.TenantId = user; } // If the tokens have expired or become invalid for any reason, ask the user to sign in again catch (MsalUiRequiredException ex) { return(new RedirectResult("/Account/SignIn")); } //Handle unexpected errors. catch (Exception ex) { } return(Json(ViewBag.TenantId, JsonRequestBehavior.AllowGet)); }