【发布时间】:2021-03-04 13:21:22
【问题描述】:
我创建了一个 API,并期待来自 Azure AD 的访问令牌:
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Audience = Configuration["AzureAd:ClientId"];
options.Authority = $"{Configuration["AzureAd:Instance"]}{Configuration["AzureAd:TenantId"]}";
});
然后我使用 MSAL 在 React 上创建了一个 SPA,但是当我登录时,我的 API 不接受从 MSAL 返回的访问令牌:
function RequestProfileData() {
instance.acquireTokenSilent({
...loginRequest,
account: accounts[0]
}).then((response) => {
debugger;
const api = axios.create({
baseURL: 'https://localhost:44312',
headers: {
authorization: `Bearer ${response.accessToken}`
}
});
api.get('/WeatherForecast').then(result => {
debugger;
console.log(result.data)
});
})
我的 MSAL 配置是:
export const msalConfig = {
auth: {
clientId: "ClientId",
authority: "https://sts.windows.net/TenantId/",
tenantId: "TenantId",
redirectUri: "http://localhost:3000/",
"
},
cashe: {
casheLocation: "sessionStorage",
storeAuthStateInCookkie: false
},
}
export const loginRequest = {
scope: ['api://ClientId/Read']
}
【问题讨论】:
-
请问您是如何获得令牌的?
-
我使用 MSAL,这是我的代码 const { instance, accounts } = useMsal(); 配置:msalConfig = { auth: { clientId: "clientId", authority: "sts.windows.net/APITenantId", tenantId: "API TenantId" , redirectUri: "localhost:3000", client_Secret: "ClientSecret" }, Cashe: { CasheLocation: "sessionStorage", storeAuthStateInCookkie: false }, } export const loginRequest = { scope: ['api://clientId/Read'] }
-
对不起我是初学者:(
-
我可以知道代码范围内的 clientId 值是否:['api://clientId/Read'] 与服务器端相同:options.Audience = Configuration["AzureAd:ClientId"]; ?
-
是一样的
标签: azure azure-active-directory