IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

401 Unauthorize In API

Open mehmetuzer opened this issue 6 years ago • 7 comments

Question

Hello to everyone,

I am using identityserver4. I login successfully but I couldn't verify access token in api. I get an unauthorized login error. What would be the reason ?

API

   services.AddAuthentication("Bearer")
               .AddJwtBearer("Bearer", options =>
               {
                   options.SaveToken = true;
                   options.Authority = "http://localhost:5000";
                   options.RequireHttpsMetadata = false;
                   options.TokenValidationParameters = new TokenValidationParameters
                   {
                       ValidateIssuerSigningKey = false,
                       ValidateIssuer = false,
                       ValidateAudience = false
                   };
                   options.Audience = "quality";
               });

Appsettings Info

  "IssuerUri": "urn:sso.company.com",
 "IdentityResources": [
      {
        "Name": "openid",
        "DisplayName": "Your user identifier",
        "Required": true,
        "UserClaims": [ "sub" ]
      },
      {
        "Name": "profile",
        "DisplayName": "User profile",
        "Description": "Your user profile information (first name, last name, etc.)",
        "Emphasize": true,
        "UserClaims": [ "name", "family_name", "given_name", "middle_name", "preferred_username", "profile", "picture", "website", "gender", "birthdate", "zoneinfo", "locale", "updated_at", "role" ]
      },
      {
        "Name": "custom.profile",
        "DisplayName": "Custom User profile",
        "Description": "Your custom user profile information",
        "Emphasize": true,
        "UserClaims": [ "IsFirstLogin", "EmployeeName", "role" ]
      }
    ],
    "ApiResources": [
      {
        "Name": "quality",
        "DisplayName": "Quality Resource",
        "UserClaims": [ "id", "name" ]
      }
    ],
    "Clients": [
      {
        "Enabled": true,
        "ClientId": "oidc",
        "ClientName": "Oidc Client",
        "AllowedGrantTypes": [ "authorization_code" ],
        "AllowedScopes": [ "openid", "profile", "custom.profile", "quality" ],
        "RedirectUris": [
          "http://localhost:5003/callback.html",
          "http://localhost:5003/popup.html",
          "http://localhost:5003/silent-renew.html",
          "http://localhost:5003/signin-callback.html",
          "http://localhost:5003/app/home",
          "http://localhost:5003/auth/login-page"
        ],
        "RequireConsent": true,
        "RequirePkce": true,
        "RequireClientSecret": false,
        "AccessTokenLifetime": 43200,
        "PostLogoutRedirectUris": [ "http://localhost:5003" ],
        "AllowOfflineAccess": true
      }
    ]

mehmetuzer avatar May 14 '20 08:05 mehmetuzer

Hi, actually, I've got the SAME problem as you, could you reproduce the request giving you 401 with Fiddler & Postman? I'm getting 401 with message: Bearer error="invalid_token", error_description="The audience 'empty' is invalid" found in WWW-Authenticate header, checked already my access_token at jwt.io and found what it does not have any "aud" in it. Maybe you've got the same issue as I? Then we would have just to populate access_token with valid audience and it might work? image

Here's my AddAuthentication method in my API & app.UseAuthentication/app.UseAuthorization: https://pastebin.com/YdE3WQ7b

and my client config: https://pastebin.com/AdAjntjc

Moreover, as I checked in Postman, id_token may give an access to Post Create Delete actions... Should it work like that? I'm confused

mariuszbyahoo avatar May 14 '20 08:05 mariuszbyahoo

Resolved

I don't know how but it is solved

mehmetuzer avatar May 14 '20 13:05 mehmetuzer

well, I have still this issue, currently for development purposes I am using id_token to gain access to several resources (Update, Delete, Create actions) and the question is, should I open a new issue here on GH or just wait for response? (I'm new to creating issues in GH)

EDIT*** I solved the problem already.

mariuszbyahoo avatar May 14 '20 15:05 mariuszbyahoo

I am using id_token to gain access to several resources

This is not the right usage of an id_token

leastprivilege avatar May 30 '20 13:05 leastprivilege

@mariuszbyahoo @mehmetuzer Any clue how you solved the empty audience?

molinch avatar Jun 02 '20 06:06 molinch

Yup, I forgot about an API Resource, when I added it, and added it to my client's allowed scopes my access token gain this required aud resource. image

mariuszbyahoo avatar Jun 02 '20 07:06 mariuszbyahoo

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Nov 26 '20 23:11 github-actions[bot]