private static string serviceBaseAddress = "http://localhost:60723/"; // base url of the web api public async Task <ActionResult> Index() { string signedInUserID = ClaimsPrincipal.Current.FindFirst(JwtRegisteredClaimNames.Sub).Value; var confidentialClientApplication = MSALAppBuilder.GetAppBuilder(signedInUserID, this.HttpContext); var userRole = User.IsInRole("Admin"); var user = await confidentialClientApplication.GetAccountsAsync(); AuthenticationResult result = await confidentialClientApplication.AcquireTokenSilentAsync(new List <string>() { AuthenticationConfig.scopes }, user.FirstOrDefault(), AuthenticationConfig.authority, false); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, serviceBaseAddress + "values/GetLocation"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); HttpResponseMessage response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { string apiResponse = await response.Content.ReadAsStringAsync(); ViewBag.coordinates = apiResponse; return(View("Index")); } else { string apiResponse = await response.Content.ReadAsStringAsync(); if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { } ViewBag.ErrorMessage = "AuthorizationRequired"; return(View("Index")); } }
public async Task <ActionResult> AssignRole() { string signedInUserID = ClaimsPrincipal.Current.FindFirst(JwtRegisteredClaimNames.Sub).Value; var confidentialClientApplication = MSALAppBuilder.GetAppBuilder(signedInUserID, this.HttpContext); var user = await confidentialClientApplication.GetAccountsAsync(); AuthenticationResult result = await confidentialClientApplication.AcquireTokenSilentAsync(new List <string>() { AuthenticationConfig.azureManagementScope }, user.FirstOrDefault(), AuthenticationConfig.authority, false); var roleAssignmentModel = new RoleAssignmentModel() { SubscriptionId = "47ca3602-b986-46de-a99a-e473c26bd588", ResourceGroupName = "AdityaAzureRG", StorageAccountName = "researchstorageacct", ContainerName = "research", RoleId = "2a2b9908-6ea1-4ae2-8e65-a410df84e7d1", ServicePrincipalObjectId = "04f300cc-5632-4820-8fd2-9d36e7efd020", RoleAssignmentName = Guid.NewGuid().ToString() }; var roleAssignmentService = new RoleAssignmentService(roleAssignmentModel); var httpResponseMessage = await roleAssignmentService.AssignRole(result.AccessToken); var responeFromazureAPi = await httpResponseMessage.Content.ReadAsStringAsync(); var deserializeResponse = JsonConvert.DeserializeObject(responeFromazureAPi).ToString(); return(View("AssignRole", (object)deserializeResponse)); }
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification) { // Extract the code from the response notification var code = notification.Code; string signedInUserID = notification.AuthenticationTicket.Identity.FindFirst(JwtRegisteredClaimNames.Sub).Value; var confidentialClientApplication = MSALAppBuilder.GetAppBuilder(signedInUserID, notification.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase); try { AuthenticationResult result = await confidentialClientApplication.AcquireTokenByAuthorizationCodeAsync(code, new List <string>() { AuthenticationConfig.scopes }); } catch (MsalException ex) { throw; } catch (Exception ex) { //TODO: Handle throw; } }