Reputation: 61
I'm having issues getting an access token from my React app, trying to access my own .NET Core 3.1 web API. I'm able to login fine and can access the user data, but when I try to get an access token, I get the below error continuously. Would appreciate any help or push in the right direction. Thank you!
Uncaught (in promise) BrowserAuthError: silent_sso_error: Silent SSO could not be completed - insufficient information was provided. Please provide either a loginHint or sid.
Here's what I have:
.NET Core 3.1 Web API setup (ref: https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-protected-web-api-app-configuration):
services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAdB2C");
"AzureAdB2C": {
"Instance": "https://{example}.b2clogin.com",
"ClientId": "{web api Client ID}",
"TenantId": "xxxxxxxxxxxx",
"Audience": "https://{example}.onmicrosoft.com/api"
}
React set up using "@azure/msal-browser": "^2.7.0" and "@azure/msal-react": "^1.0.0-alpha.0" (ref: https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-react):
const configuration: Configuration = {
auth: {
clientId: '{SPA Client ID}',
authority: 'https://{example}.b2clogin.com/{example}.onmicrosoft.com/B2C_1_SignUpIn',
knownAuthorities: ['{example}.b2clogin.com'],
},
cache: {
cacheLocation: 'localStorage', // This configures where your cache will be stored
storeAuthStateInCookie: false // Set this to "true" to save cache in cookies
}
};
const pca = new PublicClientApplication(configuration);
<MsalProvider instance={pca}>
<App />
</MsalProvider>
useEffect(() => {
if (error) {
login(InteractionType.Popup);
}
}, [error, login]);
const { instance, accounts, inProgress } = useMsal();
useEffect(() => {
if (inProgress === 'none' && accounts.length > 0) {
const request = {
account: accounts[0],
scopes: ["https://{example}.onmicrosoft.com/api/scope.read"]
};
// Retrieve an access token
instance.acquireTokenSilent(request)
.then(response => {
console.log('response', response);
if (response.accessToken) {
console.log('accessToken', response.accessToken);
return response.accessToken;
}
return null;
})
.catch(error => console.log('token error', error));
}
}, [inProgress, accounts, instance]);
Upvotes: 4
Views: 6351
Reputation: 61
For anyone who is having a similar issue, the work around is to pass in the login request with scopes in the initial login. In my case, I'm using useMsalAuthentication() hook to log in. You can follow the thread here, https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/2702.
export const loginRequest = {
scopes: [ "openid", "https://{example}.onmicrosoft.com/api/scope.read" ]
}
const {login, error} = useMsalAuthentication(InteractionType.Redirect, loginRequest);
Upvotes: 2
Reputation: 7483
The documents that you referred are all about Azure AD, but not Azure AD B2C.
We use react-azure-adb2c
library to use ReactJS with Azure AD B2C. You could follow this blog, and it shows you the steps and sample code.
Upvotes: 1