Asp.net core azure b2c cookie authentication on client

2

I have a web api written in asp.net core, and using Azure AD B2C for authentication. Everything works just fine in the browser: the endpoints with [Authorize] attribute require login, so the browser redirects me to the Azure login page, and after I'm logged in I got the answer from the api.

For the client I'm building a UWP app. I managed to get the Access Token using Microsoft.Identity.Client Nuget package, but I can't get send the request using the token. Everytime I'm trying I get an exception (I't thrown in the client code below):

System.Runtime.InteropServices.COMException (0x80072F88): The HTTP redirect request must be confirmed by the user at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpHandlerToFilter.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClientHandler.d__86.MoveNext()

If I remove the [Autherize] attribute from the endpoint, it works just fine.

My server side code:

Startup.cs -> ConfigureServices:

services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddAzureAdB2C(options => { Configuration.Bind("AzureAdB2C", options); })
        .AddCookie();

So I'm using Cookie authentication scheme. But from the client I can't properly attach the access token.

My client code:

var httpClient = new HttpClient();
        HttpResponseMessage response;
        try
        {

            var request = new HttpRequestMessage(HttpMethod.Delete, uri);
            request.Headers.Add("Cookie", ".AspNetCore.Cookies=" + token);
            //request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
            response = await httpClient.SendAsync(request);
            if (response.StatusCode != System.Net.HttpStatusCode.NoContent)
            {
                throw new Exception();
            }
        }
        catch (Exception ex)
        {
            throw new Exception();
        }

The login work perfectly in UWP as well, but after it I can't do anything with the Authorized endpoints.

So how should I attach the access token to my http request to authorize properly using the given scheme?

c#
asp.net
azure
uwp
azure-ad-b2c
asked on Stack Overflow Jun 4, 2018 by Goba

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0