Summary
I try to implement Azure AD B2C authentication in our project. We have a react Frontend and a .net core Backend (REST API's).
How it should be:
- Open Frontend -> Redirect to microsoft b2c site (works)
- Enter credential and get the token from Azure B2c (works)
- Redirect to our Frontend (works)
- Call a REST API on the Backend (works) I can see the token in the HttpContext Header (see Link "Token in Header") so the token will be delivered from our frontend to the backend properly. Token in Header
- Gets the Identity and claims information out of the httpContext. (doesn't work). -> The user is not set property, no Identity, no claims, nothing (see Link "User Identity/Claims") User Identity/Claims
This my Startup.cs configuration. -> This is the example from Microsoft on github with b2c configuration.
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
// Handling SameSite cookie according to https://learn.microsoft.com/en-us/aspnet/core/security/samesite?view=aspnetcore-3.1
options.HandleSameSiteCookieCompatibility();
});
// Configuration to sign-in users with Azure AD B2C
services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C");
services.AddControllers();
//Configuring appsettings section AzureAdB2C, into IOptions
services.AddOptions();
services.Configure<OpenIdConnectOptions>(Configuration.GetSection("AzureAdB2C"));
This is my token which I get from Azure AD B2c. (see Link "Token") Token
This is my application setting (see Link "Settings") Settings
I also tried this -> Azure AD B2C Token returns name but User.Identity.Name is null But doesn't work.
Can Some tell me what I am doing wrong? Why is the User Identiy/Claims always null?
React code for axios calls handling. Adding the token to the header:
export function buildHTTPHeaders(config: AxiosConfig) {
const headers: { [key: string]: string } = {
Accept: CONTENT_TYPE,
'Content-Type': CONTENT_TYPE,
};
const apiToken = sessionStorage.getItem('msal.idtoken');
const auth = apiToken ? `Bearer ${apiToken}` : config.authorization;
if (auth) {
headers['Authorization'] = auth;
}
return headers;
}
export const getApiBaseURL = () => temporaryStaticConfig.API_BASE_PATH;
export function getAxios(config: AxiosConfig = { throwNotFoundException: false }) {
const { dispatch, getState } = store;
const instance = Axios.create({
validateStatus: validateStatus(dispatch, getState),
baseURL: getApiBaseURL(),
headers: buildHTTPHeaders(config),
});
instance.interceptors.response.use(transformNotFound(config.throwNotFoundException));
return instance;
}