Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate IdentityServer4 with API single project in actual combat

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report