1

Hi All,

I got a scenario in which i am supposed to call a REST api that is secured by a AZURE ACTIVE DIRECTORY. Most of the code runs fine and i am able to get the token using myMSALObj.acquireTokenSilent function too.

401 ERROR COMES WHEN I SEND AJAX CALL USING THAT TOKEN, YOU CAN SEE FOLLOWING CODE

User is there in Active Directory for which i get proper token, i dont know why my ajax call fails when i send that token to rest-api, please help

    
<script type="text/javascript">

    const msalConfig = {
        auth: {
            clientId: "94edf294-08ae-489f-8621-c6d98009afa8", 
            authority: "https://login.microsoftonline.com/c483b3c4-21a6-4c93-95ea-7436cf318a68",
            redirectUri: "https://localhost:44334/",
        },
        cache: {
            cacheLocation: "sessionStorage", // This configures where your cache will be stored
            storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
        }
    };

    const myMSALObj = new Msal.UserAgentApplication(msalConfig);

    function CallApi() {
        // Add scopes for the id token to be used at Microsoft identity platform endpoints.
        const loginRequest = {
            scopes: ["User.Read"],
        };

        myMSALObj.loginPopup(loginRequest)
            .then((loginResponse) => {

                const accessTokenRequest = {
                    scopes: ["api://8c2b0253-f9e8-442c-bccf-b4a8bbe73b59/access_as_user"]
                };

                myMSALObj.acquireTokenSilent(accessTokenRequest).then((accessTokenResponse) => {
                    var accessToken = accessTokenResponse.accessToken;
                    var apiEndpoint = "https://localhost:44387/api/hello";
                    var bearer = "Bearer " + accessToken;
                    console.log("accessToken = ", accessToken);
                    $.ajax({
                        url: apiEndpoint,
                        type: "GET",
                        beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", bearer) }
                    }).done(function (response) {
                        alert("SUCCESS");
                        console.log("response = ", JSON.stringify(response));
                    }).fail(function (err) {
                        console.error("Error Occurred");
                        console.log("error = ", JSON.stringify(err));
                    });

                })

            }).catch(function (error) {
                console.log(error);
            });
    }
</script>

Screenshot of a NEW API Code enter image description here

Screenshot of JWT.ms (Access Token) enter image description here enter image description here

New Screenshot of JWT Token enter image description here

4

2 回答 2

1

You should not set the client ID in the appsettings.json file to the client id of your api app. It should be the client id of your client app. According to the jwt analysis diagram you provided, I think it should be: 94edf294- 08ae-489f-8621-c6xxxxxxx.

enter image description here

于 2020-09-21T02:12:01.207 回答
0

My bad, Following call was missing in my startup.cs file

app.UseAuthentication();

thanks for your help guys

Specially - @Carl Zhao, @Purushothaman @Rass Masood

于 2020-09-24T00:08:49.310 回答