您好,登錄后才能下訂單哦!
IdentityServer4實戰(zhàn)中怎么與API單項目整合,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
我們在實際使用 IdentityServer4 的時候,可能會在使用 IdentityServer4 項目添加一些API,比如 找回密碼、用戶注冊、修改用戶資料等,這些API與IdentityServer4怎么共存在一個項目呢?
1.首先在 Startup.cs
中添加 IdentityServer4
services.AddIdentityServer(options=>options.Authentication.CookieAuthenticationScheme= "Cookies") .AddDeveloperSigningCredential() .AddInMemoryIdentityResources(Config.GetIdentityResources()) .AddInMemoryApiResources(Config.GetApis()) .AddInMemoryClients(Config.GetClients()) .AddTestUsers(Config.GetUsers());
2.然后在添加 IdentityServer4 下添加認證
services.AddAuthentication("Bearer") .AddCookie("Cookies") .AddJwtBearer("Bearer", options => { //identityserver4 地址 也就是本項目地址 options.Authority = "http://localhost:5000"; options.RequireHttpsMetadata = false; options.Audience = "api1"; });
注意事項:
Cookie Scheme 是非必須的,但是如果不設(shè)置會報錯,但是也不會影響正常使用
AddAuthentication
必須必須必須 放在 AddIdentityServer
之后
3.中間件配置
app.UseIdentityServer();
這里只需 UseIdentityServer 即可
在 IdentityServer4 項目添加一個 Controller
[Route("identity")]
[Authorize]public class IdentityController : ControllerBase{
public IActionResult Get() {
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}
將 IdentityServer4 項目的端口設(shè)置為5000,使用密碼模式,下面進行測試:
1.請求Token
2.請求API
關(guān)于IdentityServer4實戰(zhàn)中怎么與API單項目整合問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。