Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to integrate IdentityServer4 with API single project in actual combat

Shulou Source: shulou.com Published: 2022-06-01 06:41:07 10月05日 Update

How to integrate IdentityServer4 with API single project in actual combat? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

one。 Preface

When we actually use IdentityServer4, we may use IdentityServer4 projects to add some API, such as retrieving passwords, user registration, changing user profiles, and so on. How can these API and IdentityServer4 share a project?

two。 Integration

1. First add IdentityServer4 to Startup.cs

Services.AddIdentityServer (options= > options.Authentication.CookieAuthenticationScheme= "Cookies") .AddDeveloperSigningCredential () .AddInMemoryIdentityResources (Config.GetIdentityResources ()) .AddInMe moryApiResources (Config.GetApis ()) .AddInMemoryClients (Config.GetClients ()) .AddTestUsers (Config.GetUsers ())

two。 Then add authentication under add IdentityServer4

Services.AddAuthentication ("Bearer") .AddCookie ("Cookies") .AddJwtBearer ("Bearer", options = > {/ / identityserver4 address, i.e. the project address options.Authority = "http://localhost:5000"; options.RequireHttpsMetadata = false; options.Audience =" api1 " })

Note:

Cookie Scheme is not necessary, but if it is not set, an error will be reported, but it will not affect normal use.

AddAuthentication must be placed after AddIdentityServer

3. Middleware configuration

App.UseIdentityServer ()

All you need here is UseIdentityServer.

three。 test

Add a Controller to the IdentityServer4 project

[Route ("identity")] [Authorize] public class IdentityController: ControllerBase {

Public IActionResult Get () {

Return new JsonResult (from c in User.Claims select new {c.Type, c.Value});}}

Set the port of the IdentityServer4 project to 5000, use password mode, and test it below:

1. Request Token

two。 Request API

This is the answer to the question about how to integrate IdentityServer4 with API in actual combat. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Tags: Project integration question actual combat address password more user help test answer easy easy middleware that is item content preface just actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS MySQL Shulou Technology Xiaomi