简体   繁体   中英

OAuth Implementation 2.0 Using .Net Core , Swagger

I want to implement the OAuth 2.0 in my web application. In web application, I have used swagger and I implemented the following code.

c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Type = SecuritySchemeType.OAuth2,
                    Flows = new OpenApiOAuthFlows
                    {
                        Implicit = new OpenApiOAuthFlow
                        {
                            AuthorizationUrl = new Uri("https://myWebAppUrl", UriKind.Absolute),
                            TokenUrl = new Uri("https://myWebAppUrl"),
                        }
                    }
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement 
                {
                    { 
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
                        },
                        new[] { "readaccess", "writeaccess" }
                    }
                });

I added the above code and ran my project. Then, On the click of Authorize I was able to see the "Authorize" button and When I clicked on "Authorize" button, I was redirected to my web app login page.

Now, I want to validate the login credentials that the user will enter on the login page, So, I generated JWT token in my login API to validate the user's credentials.

I may have missed some steps here.

But, I am lost how to redirect back to the "Swagger Documentation" after the login is successful. After the login is successful it redirects me to the dashboard (I don't want this).

Please help how can I proceed.

If you are using Identity Server than you can create additional client for swagger and set redirect uri to swagger. You can start with tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM