How to JWT authenticate through Swagger with Special Json data (problem: Bearer undefined)
Asked Answered
B

0

0

My Controller(AccountController) have a Action(Login) For Authentication and This Is My Program.Cs (I'm Using ASP.NET Core 8.0) :

//...
 services.AddSwaggerGen(option => 
{ 
option.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme
 {
     Type = SecuritySchemeType.OAuth2,
     Flows = new OpenApiOAuthFlows
     {
         Password = new OpenApiOAuthFlow
         {
             TokenUrl = new Uri($"{siteUrl}/v1/AccountController/Login")
         } 
     }
 });
 option.OperationFilter<UnauthorizedResponsesOperationFilter>(true, "OAuth2");
};
//...

everything is Okey If "https://WebApplication/AccountController/Login" Return Json Like This :

{
       "access_token":"2YotnFZFEjr1zCWpAA",
       "token_type":"bearer"
}

But My Proje Is Big... and My Action Should Return This :

{
  "data": {
    "access_token": "mh-1ea5_ssqrL9g",
    "refresh_token": "792bdbd30145260c",
    "token_type": "Bearer",
    "expires_in": 1999
  },
  "isSuccess": true,
  "statusCode": "success",
  "message": "Login Was Success"
}

Once logged(Left Image), the API EndPoints show the locked padlock but when I try to use them, I see the call is done without the returned bearer: : Bearer undefined(Right Image) enter image description here

Boydboyden answered 4/5 at 2:31 Comment(3)
Your "should return this" response example is not OAuth 2.0-compliant, that's why it doesn't work.Conjecture
@Conjecture Yes I Know Its not compliant... but i cant change Return Model because It's a big DDD project and I am only responsible for this part...Boydboyden
@Conjecture can i customize this swagger about this Issue??Boydboyden

© 2022 - 2024 — McMap. All rights reserved.