C# Azure B2C React(前端)和#x2B。HttpContext中的net core(后端)用户/标识/声明为空
总结 我尝试在我们的项目中实现Azure AD B2C身份验证。我们有一个react前端和一个.net核心后端(RESTAPI) 应该如何:C# Azure B2C React(前端)和#x2B。HttpContext中的net core(后端)用户/标识/声明为空,c#,.net,reactjs,azure-ad-b2c,C#,.net,Reactjs,Azure Ad B2c,总结 我尝试在我们的项目中实现Azure AD B2C身份验证。我们有一个react前端和一个.net核心后端(RESTAPI) 应该如何: 打开前端->重定向到microsoft b2c站点(works) 输入凭证并从Azure B2c(works)获取令牌 重定向到我们的前端(works) 在后端调用RESTAPI(works)我可以在HttpContext头中看到令牌(请参阅链接“头中的令牌”),因此令牌将从前端正确地传递到后端。 从httpContext中获取标识和声明信息(不工作)。
{
// 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://docs.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"));
您能否共享调用后端.net api以传递令牌的react代码?另外,您在b2c中使用userflow还是自定义策略?当然,我添加了react代码,我们在b2c中使用userflow。@无限根据您提供的代码,我认为您使用
id\u token
调用api,请使用access token调用api。@JimXu我们比较了accessToken和idToken,但它们是相同的,正常吗?@Infinity accessToken和idToken不同。Idtoken用于验证用户是否是他们声称的用户,并获取关于他们的其他有用信息。AccessToken用于使客户端能够安全地调用受保护的API。他们有不同的主张:和
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;
}