I am able to retrieve user's token in UWP with code like this
string MicrosoftClientID = {ClientID}; string MicrosoftCallbackURL = "urn:ietf:wg:oauth:2.0:oob"; string scope = WebUtility.UrlEncode("openid offline_access https://graph.microsoft.com/user.read"); string MicrosoftURL = "https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=" + MicrosoftClientID + "&response_type=code&redirect_uri=" + MicrosoftCallbackURL + "&response_mode=query&scope=" + scope; Uri StartUri = new Uri(MicrosoftURL); Uri EndUri = new Uri(MicrosoftCallbackURL); WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.None, StartUri, EndUri); if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) { string code = WebAuthenticationResult.ResponseData.Replace("urn:ietf:wg:oauth:2.0:oob?code=", ""); string strContent = "client_id=" + MicrosoftClientID + "&scope=" + scope + "&code=" + code + "&redirect_uri=" + MicrosoftCallbackURL + "&grant_type=authorization_code"; HttpClient httpClient = new HttpClient(); HttpContent httpContent = new StringContent(strContent); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); HttpResponseMessage httpResponseMessage = await httpClient.PostAsync("https://login.microsoftonline.com/consumers/oauth2/v2.0/token", httpContent); string stringResponse = await httpResponseMessage.Content.ReadAsStringAsync(); }
but how can i use the token to make a request to API of my .NET Core Web application, which is hosted on azure?
My startup.cs and homecontroller.cs look like below
app.UseOAuthAuthentication(new OAuthOptions() { AuthenticationScheme = "Microsoft-AccessToken", DisplayName = "MicrosoftAccount-AccessToken", ClientId = {CliendID}, ClientSecret = {ClientSecret}, CallbackPath = new PathString("/signin-microsoft-token"), AuthorizationEndpoint = MicrosoftAccountDefaults.AuthorizationEndpoint, TokenEndpoint = MicrosoftAccountDefaults.TokenEndpoint, UserInformationEndpoint = MicrosoftAccountDefaults.UserInformationEndpoint, Scope = { "https://graph.microsoft.com/user.read" }, SaveTokens = true, Events = new OAuthEvents() { OnCreatingTicket = async context => { var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted); response.EnsureSuccessStatusCode(); var user = JObject.Parse(await response.Content.ReadAsStringAsync()); var identifier = user.Value<string>("id"); if (!string.IsNullOrEmpty(identifier)) { context.Identity.AddClaim(new Claim( ClaimTypes.NameIdentifier, identifier, ClaimValueTypes.String, context.Options.ClaimsIssuer)); } var userName = user.Value<string>("displayName"); if (!string.IsNullOrEmpty(userName)) { context.Identity.AddClaim(new Claim( ClaimTypes.Name, userName, ClaimValueTypes.String, context.Options.ClaimsIssuer)); } var email = user.Value<string>("userPrincipalName"); if (!string.IsNullOrEmpty(email)) { context.Identity.AddClaim(new Claim( ClaimTypes.Email, email, ClaimValueTypes.Email, context.Options.ClaimsIssuer)); } } } });
[Authorize] public string GetInfo() { return "Hello world!"; }
I have tried these
httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationModel.AccessToken); string apicontent = await httpClient.GetStringAsync("https://{host}.azurewebsites.net/home/GetInfo");
all I got is html of login page
Any help please?