日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IdentityServer4与ocelot实现认证与客户端统一入口

發(fā)布時間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IdentityServer4与ocelot实现认证与客户端统一入口 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關于IdentityServer4與ocelot博客園里已經(jīng)有很多介紹我這里就不再重復了。

ocelot與IdentityServer4組合認證博客園里也有很多,但大多使用ocelot內置的認證,而且大多都是用來認證API的,查找了很多資料也沒看到如何認證oidc,所以這里的ocelot實際只是作為統(tǒng)一入口而不參與認證,認證的完成依然在客戶端。代碼是使用IdentityServer4的Quickstart5_HybridAndApi 示例修改的。項目結構如下

?

一 ocelot網(wǎng)關

我們先在示例添加一個網(wǎng)關。

修改launchSettings.json中的端口為54660

配置文件如下

{

? "ReRoutes": [

? ? { // MvcClient

? ? ? "DownstreamPathTemplate": "/MvcClient/{route}",

? ? ? "DownstreamScheme": "http",

? ? ? "DownstreamHostAndPorts": [

? ? ? ? {

? ? ? ? ? "Host": "localhost",

? ? ? ? ? "Port": 50891

? ? ? ? }

? ? ? ],

? ? ? "UpstreamPathTemplate": "/MvcClient/{route}",

? ? ? "UpstreamHeaderTransform": {

? ? ? ? "X-Forwarded-For": "{RemoteIpAddress}"

? ? ? }

? ? },

? ? { // signin-oidc

? ? ? "DownstreamPathTemplate": "/signin-oidc",

? ? ? "DownstreamScheme": "http",

? ? ? "DownstreamHostAndPorts": [

? ? ? ? {

? ? ? ? ? "Host": "localhost",

? ? ? ? ? "Port": 50891

? ? ? ? }

? ? ? ],

? ? ? "UpstreamPathTemplate": "/signin-oidc",

? ? ? "UpstreamHeaderTransform": {

? ? ? ? "X-Forwarded-For": "{RemoteIpAddress}"

? ? ? }

? ? },

? ? { // signout-callback-oidc

? ? ? "DownstreamPathTemplate": "/signout-callback-oidc",

? ? ? "DownstreamScheme": "http",

? ? ? "DownstreamHostAndPorts": [

? ? ? ? {

? ? ? ? ? "Host": "localhost",

? ? ? ? ? "Port": 50891

? ? ? ? }

? ? ? ],

? ? ? "UpstreamPathTemplate": "/signout-callback-oidc",

? ? ? "UpstreamHeaderTransform": {

? ? ? ? "X-Forwarded-For": "{RemoteIpAddress}"

? ? ? }

? ? },

? ? { // MyApi

? ? ? "DownstreamPathTemplate": "/MyApi/{route}",

? ? ? "DownstreamScheme": "http",

? ? ? "DownstreamHostAndPorts": [

? ? ? ? {

? ? ? ? ? "Host": "localhost",

? ? ? ? ? "Port": 50890

? ? ? ? }

? ? ? ],

? ? ? "UpstreamPathTemplate": "/MyApi/{route}",

? ? ? "UpstreamHeaderTransform": {

? ? ? ? "X-Forwarded-For": "{RemoteIpAddress}"

? ? ? }

? ? },

? ? { // IdentityServer

? ? ? "DownstreamPathTemplate": "/{route}",

? ? ? "DownstreamScheme": "http",

? ? ? "DownstreamHostAndPorts": [

? ? ? ? {

? ? ? ? ? "Host": "localhost",

? ? ? ? ? "Port": 50875

? ? ? ? }

? ? ? ],

? ? ? "UpstreamPathTemplate": "/IdentityServer/{route}",

? ? ? "UpstreamHeaderTransform": {

? ? ? ? "X-Forwarded-For": "{RemoteIpAddress}"

? ? ? }

? ? },

? ? { // IdentityServer

? ? ? "DownstreamPathTemplate": "/{route}",

? ? ? "DownstreamScheme": "http",

? ? ? "DownstreamHostAndPorts": [

? ? ? ? {

? ? ? ? ? "Host": "localhost",

? ? ? ? ? "Port": 50875

? ? ? ? }

? ? ? ],

? ? ? "UpstreamPathTemplate": "/{route}",

? ? ? "UpstreamHeaderTransform": {

? ? ? ? "X-Forwarded-For": "{RemoteIpAddress}"

? ? ? }

? ? }

? ]

}

這里我們定義3個下游服務,MvcClient,MyApi,IdentityServer,并使用路由特性把signin-oidc,signout-callback-oidc導航到MvcClient,由MvcClient負責生成最后的Cooike。并將默認路由指定到IdentityServer服務。


在ConfigureServices中添加Ocelot服務。

services.AddOcelot()

? ? ? ? ? ? .AddCacheManager(x =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? x.WithDictionaryHandle();

? ? ? ? ? ? })

? ? ? ? ? ? .AddPolly()

在Configure中使用Ocelot中間件

app.UseOcelot().Wait();

Ocelot網(wǎng)關就部署完成了。

二 修改QuickstartIdentityServer配置

首先依然是修改launchSettings.json中的端口為50875

在ConfigureServices中修改AddIdentityServer配置中的PublicOrigin和IssuerUri的Url為http://localhost:54660/IdentityServer/

services.AddIdentityServer(Option =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Option.PublicOrigin = "http://localhost:54660/IdentityServer/";

? ? ? ? ? ? ? ? Option.IssuerUri = "http://localhost:54660/IdentityServer/";

? ? ? ? ? ? })

? ? ? ? ? ? ? ? .AddDeveloperSigningCredential()

? ? ? ? ? ? ? ? .AddInMemoryIdentityResources(Config.GetIdentityResources())

? ? ? ? ? ? ? ? .AddInMemoryApiResources(Config.GetApiResources())

? ? ? ? ? ? ? ? .AddInMemoryClients(Config.GetClients())

? ? ? ? ? ? ? ? .AddTestUsers(Config.GetUsers());


這樣一來發(fā)現(xiàn)文檔中的IdentityServer地址就變?yōu)榫W(wǎng)關的地址了,進一步實現(xiàn)IdentityServer的負載均衡也是沒有問題的。

修改Config.cs中mvc客戶端配置如下

ClientId = "mvc",

? ? ? ? ? ? ? ? ? ? ClientName = "MVC Client",

? ? ? ? ? ? ? ? ? ? AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

? ? ? ? ? ? ? ? ? ? ClientSecrets =

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? new Secret("secret".Sha256())

? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ?// AccessTokenType = AccessTokenType.Reference,

? ? ? ? ? ? ? ? ? ? RequireConsent = true,

? ? ? ? ? ? ? ? ? ? RedirectUris = { "http://localhost:54660/signin-oidc" },

? ? ? ? ? ? ? ? ? ? PostLogoutRedirectUris = { "http://localhost:54660/signout-callback-oidc" },

? ? ? ? ? ? ? ? ? ? AllowedScopes =

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? IdentityServerConstants.StandardScopes.OpenId,

? ? ? ? ? ? ? ? ? ? ? ? IdentityServerConstants.StandardScopes.Profile,

? ? ? ? ? ? ? ? ? ? ? ? "api1"

? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? AllowOfflineAccess = true,

? ? ? ? ? ? ? ? ? ? //直接返回客戶端需要的Claims

? ? ? ? ? ? ? ? ? ? AlwaysIncludeUserClaimsInIdToken = true,

主要修改RedirectUris和PostLogoutRedirectUris為網(wǎng)關地址,在網(wǎng)關也設置了signin-oidc和signout-callback-oidc轉發(fā)請求到Mvc客戶端。

三 修改MvcClient

修改MvcClient的launchSettings.json端口為50891。

修改MvcClient的Authority地址為http://localhost:54660/IdentityServer和默認路由地址MvcClient/{controller=Home}/{action=index}/{id?}

JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();


? ? ? ? ? ? services.AddAuthentication(options =>

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? options.DefaultScheme = "Cookies";

? ? ? ? ? ? ? ? ? ? options.DefaultChallengeScheme = "oidc";

? ? ? ? ? ? ? ? ? ? //options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;

? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? .AddCookie("Cookies",options=> {

? ? ? ? ? ? ? ? ? ? options.ExpireTimeSpan = TimeSpan.FromMinutes(30);

? ? ? ? ? ? ? ? ? ? options.SlidingExpiration = true;

? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? .AddOpenIdConnect("oidc", options =>

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? options.SignInScheme = "Cookies";


? ? ? ? ? ? ? ? ? ? options.Authority = "http://localhost:54660/IdentityServer";

? ? ? ? ? ? ? ? ? ? options.RequireHttpsMetadata = false;


? ? ? ? ? ? ? ? ? ? options.ClientId = "mvc";

? ? ? ? ? ? ? ? ? ? options.ClientSecret = "secret";

? ? ? ? ? ? ? ? ? ? options.ResponseType = "code id_token";


? ? ? ? ? ? ? ? ? ? options.SaveTokens = true;

? ? ? ? ? ? ? ? ? ? options.GetClaimsFromUserInfoEndpoint = true;


? ? ? ? ? ? ? ? ? ? options.Scope.Add("api1");


? ? ? ? ? ? ? ? ? ? options.Scope.Add("offline_access");

? ? ? ? ? ? ? ? });

app.UseMvc(routes =>

? ? ? ? ? ? {

? ?

? ? ? ? ? ? ? ? ? ? routes.MapRoute(

? ? ? ? ? ? ? ? ? ? ? ? name: "default",

? ? ? ? ? ? ? ? ? ? ? ? template: "MvcClient/{controller=Home}/{action=index}/{id?}");

? ? ? ? ? ?

? ? ? ? ? ? });

修改HomeController,將相關地址修改為網(wǎng)關地址

public async Task<IActionResult> CallApiUsingClientCredentials()

? ? ? ? {

? ? ? ? ? ? var tokenClient = new TokenClient("http://localhost:54660/IdentityServer/connect/token", "mvc", "secret");

? ? ? ? ? ? var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");


? ? ? ? ? ? var client = new HttpClient();

? ? ? ? ? ? client.SetBearerToken(tokenResponse.AccessToken);

? ? ? ? ? ? var content = await client.GetStringAsync("http://localhost:54660/MyApi/identity");


? ? ? ? ? ? ViewBag.Json = JArray.Parse(content).ToString();

? ? ? ? ? ? return View("json");

? ? ? ? }


? ? ? ? public async Task<IActionResult> CallApiUsingUserAccessToken()

? ? ? ? {

? ? ? ? ? ? var accessToken = await HttpContext.GetTokenAsync("access_token");

? ? ? ? ? ? //OpenIdConnectParameterNames

? ? ? ? ? ? var client = new HttpClient();

? ? ? ? ? ? client.SetBearerToken(accessToken);

? ? ? ? ? ? var content = await client.GetStringAsync("http://localhost:54660/MyApi/identity");


? ? ? ? ? ? ViewBag.Json = JArray.Parse(content).ToString();

? ? ? ? ? ? return View("json");

? ? ? ? }

四 修改Api項目

Api項目修改多一點。

將MvcClient的HomeController和相關視圖復制過來,模擬MVC與API同時存在的項目。

修改Api的launchSettings.json端口為50890。

修改Startup

public class Startup

? ? {

? ? ? ? public void ConfigureServices(IServiceCollection services)

? ? ? ? {

? ? ? ? ? ? services.AddDataProtection(options => options.ApplicationDiscriminator = "00000").SetApplicationName("00000");


? ? ? ? ? ? services.AddMvc();

? ? ? ? ? ? JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();


? ? ? ? ? ? services.AddAuthentication(options =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? options.DefaultScheme = "Cookies";

? ? ? ? ? ? ? ? options.DefaultChallengeScheme = "oidc";

? ? ? ? ? ? }).AddCookie("Cookies")

? ? ? ? ? ? ? ?.AddOpenIdConnect("oidc", options =>

? ? ? ? ? ? ? ?{

? ? ? ? ? ? ? ? ? ?options.SignInScheme = "Cookies";


? ? ? ? ? ? ? ? ? ?options.Authority = "http://localhost:54660/IdentityServer";

? ? ? ? ? ? ? ? ? ?options.RequireHttpsMetadata = false;


? ? ? ? ? ? ? ? ? ?options.ClientId = "mvc";

? ? ? ? ? ? ? ? ? ?options.ClientSecret = "secret";

? ? ? ? ? ? ? ? ? ?options.ResponseType = "code id_token";


? ? ? ? ? ? ? ? ? ?options.SaveTokens = true;

? ? ? ? ? ? ? ? ? ?options.GetClaimsFromUserInfoEndpoint = true;


? ? ? ? ? ? ? ? ? ?options.Scope.Add("api1");

? ? ? ? ? ? ? ? ? ?options.Scope.Add("offline_access");

? ? ? ? ? ? ? ?})

? ? ? ? ? ? ? ? ? ? .AddIdentityServerAuthentication("Bearer", options =>

? ? ? ? ? ? ? ? ? ? ?{

? ? ? ? ? ? ? ? ? ? ? ? ?options.Authority = "http://localhost:54660/IdentityServer";

? ? ? ? ? ? ? ? ? ? ? ? ?options.RequireHttpsMetadata = false;

? ? ? ? ? ? ? ? ? ? ? ? ?options.ApiSecret = "secret123";

? ? ? ? ? ? ? ? ? ? ? ? ?options.ApiName = "api1";

? ? ? ? ? ? ? ? ? ? ? ? ?options.SupportedTokens= SupportedTokens.Both;

? ? ? ? ? ? ? ? ? ? ?});


? ? ? ? ? ? services.AddAuthorization(option =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //默認 只寫 [Authorize],表示使用oidc進行認證

? ? ? ? ? ? ? ? option.DefaultPolicy = new AuthorizationPolicyBuilder("oidc").RequireAuthenticatedUser().Build();

? ? ? ? ? ? ? ? //ApiController使用這個? [Authorize(Policy = "ApiPolicy")],使用jwt認證方案

? ? ? ? ? ? ? ? option.AddPolicy("ApiPolicy", policy =>

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);

? ? ? ? ? ? ? ? ? ? policy.RequireAuthenticatedUser();

? ? ? ? ? ? ? ? });

? ? ? ? ? ? });

? ? ? ? }


? ? ? ? public void Configure(IApplicationBuilder app, IHostingEnvironment env)

? ? ? ? {

? ? ? ? ? ? //var options = new ForwardedHeadersOptions

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,

? ? ? ? ? ? //? ? ForwardLimit = 1

? ? ? ? ? ? //};

? ? ? ? ? ? //options.KnownNetworks.Clear();

? ? ? ? ? ? //options.KnownProxies.Clear();

? ? ? ? ? ? //app.UseForwardedHeaders(options);

? ? ? ? ? ? //if (env.IsDevelopment())

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? app.UseDeveloperExceptionPage();

? ? ? ? ? ? //}

? ? ? ? ? ? //else

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? app.UseExceptionHandler("/Home/Error");

? ? ? ? ? ? //}


? ? ? ? ? ? app.UseAuthentication();


? ? ? ? ? ? app.UseStaticFiles();

? ? ? ? ? ? app.UseMvc(routes =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? routes.MapRoute(

? ? ? ? ? ? ? ? ? ? name: "default",

? ? ? ? ? ? ? ? ? ? template: "MyApi/{controller=MAccount}/{action=Login}/{id?}");


? ? ? ? ? ? });

? ? ? ? }

? ? }

主要添加了oidc認證配置和配置驗證策略來同時支持oidc認證和Bearer認證。

修改IdentityController中的[Authorize]特性為[Authorize(Policy = "ApiPolicy")]

?

?

?依次使用調試-開始執(zhí)行(不調試)并選擇項目名稱啟動QuickstartIdentityServer,Gateway,MvcClient,Api,啟動方式如圖

應該可以看到Gateway啟動后直接顯示了IdentityServer的默認首頁

?

在瀏覽器輸入http://localhost:54660/MVCClient/Home/index進入MVCClient


?

點擊Secure進入需要授權的頁面,這時候會跳轉到登陸頁面(才怪

實際上我們會遇到一個錯誤,這是因為ocelot做網(wǎng)關時下游服務獲取到的Host實際為localhost:50891,而在IdentityServer中設置的RedirectUris為網(wǎng)關的54660,我們可以通過ocelot轉發(fā)X-Forwarded-Host頭,并在客戶端通過UseForwardedHeaders中間件來獲取頭。但是UseForwardedHeaders中間件為了防止IP欺騙攻擊需要設置KnownNetworks和KnownProxies以實現(xiàn)嚴格匹配。當然也可以通過清空KnownNetworks和KnownProxies的默認值來不執(zhí)行嚴格匹配,這樣一來就有可能受到攻擊。所以這里我直接使用硬編碼的方式設置Host,實際使用時應從配置文件獲取,同時修改MvcClient和Api相關代碼

app.Use(async (context, next) =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? context.Request.Host = HostString.FromUriComponent(new Uri("http://localhost:54660/"));

? ? ? ? ? ? ? ? await next.Invoke();

? ? ? ? ? ? });

? ? ? ? ? ? //var options = new ForwardedHeadersOptions

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost,

? ? ? ? ? ? //? ? ForwardLimit = 1

? ? ? ? ? ? //};

? ? ? ? ? ? //options.KnownNetworks.Clear();

? ? ? ? ? ? //options.KnownProxies.Clear();

? ? ? ? ? ? //app.UseForwardedHeaders(options);


在反向代理情況下通過轉發(fā)X-Forwarded-Host頭來獲取Host地址應該時常見設置不知道還有沒有其他更好的解決辦法。

再次啟動MVCClient并輸入http://localhost:54660/MvcClient/Home/Secure。

?

?使用bob,password登陸一下


點擊Yes, Allow返回http://localhost:54660/MvcClient/Home/Secure,此時可以查看到登陸后的信息

?

?

?分別點擊Call API using user token和Call API using application identity來驗證一下通過access_token和ClientCredent模式請求來請求API

?

成功獲取到返回值。

?輸入http://localhost:54660/myapi/Home/index來查看API情況


求成功。

點擊Secure從API項目查看用戶信息,此時展示信息應該和MvcClient一致

?

嗯,并沒有看到用戶信息而是又到了授權頁.....,這是因為.netCore使用DataProtection來保護數(shù)據(jù)(點擊查看詳細信息),Api項目不能解析由MvcClient生成的Cookie,而被重定向到了IdentityServer服務中。

在MvcClient和Api的ConfigureServices下添加如下代碼來同步密鑰環(huán)。

services.AddDataProtection(options => options.ApplicationDiscriminator = "00000").SetApplicationName("00000");

?

?

再次啟動MvcClient和Api項目并在瀏覽器中輸入http://localhost:54660/MvcClient/home/Secure,此時被要求重新授權,點擊Yes, Allow后看到用戶信息

再輸入http://localhost:54660/myapi/Home/Secure從API項目查看用戶信息

?

?

?分別點擊Call API using user token和Call API using application identity來驗證一下通過access_token和ClientCredent模式請求來請求API

請求成功。?

如此我們便實現(xiàn)了通過ocelot實現(xiàn)統(tǒng)一入口,通過IdentityServer4來實現(xiàn)認證的需求?

源代碼?https://github.com/saber-wang/Quickstart5_HybridAndApi

參考

https://www.cnblogs.com/stulzq/category/1060023.html

https://www.cnblogs.com/xiaoti/p/10118930.html

https://www.cnblogs.com/jackcao/tag/identityserver4/

原文地址:https://www.cnblogs.com/nasha/p/10160695.html


.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com


總結

以上是生活随笔為你收集整理的IdentityServer4与ocelot实现认证与客户端统一入口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。