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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记

發布時間:2023/12/4 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

任務16:oauth2 + oidc 實現 client部分

實現 client 之前啟動一下上一節的 server,啟動之前需要清除一些代碼

注釋 Program 的 MigrateDbContext

public static void Main(string[] args) {BuildWebHost(args)//.MigrateDbContext<ApplicationDbContext>((context, services) => {// new ApplicationDbContextSeed().SeedAsync(context, services)// .Wait();//}).Run(); }

RegisterViewModel

[Required] //[DataType(DataType.EmailAddress)] //public string Email{get;set;} public string UserName { get; set; }

啟動程序,使用 Config 中的 TestUser 登錄

登錄成功,不過現在是在本地,接下來需要把它放到客戶端里面

新建一個 Asp.Net Core MVC 網站 MvcClient

在 startup 的 ConfigureServices 中添加 Authentication

// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) {services.Configure<CookiePolicyOptions>(options =>{// This lambda determines whether user consent for non-essential cookies is needed for a given request.options.CheckConsentNeeded = context => true;options.MinimumSameSitePolicy = SameSiteMode.None;});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);services.AddAuthentication(options =>{options.DefaultScheme = "Cookies";options.DefaultChallengeScheme = "oidc";}).AddCookie("Cookies").AddOpenIdConnect("oidc", options =>{options.SignInScheme = "Cookies";options.Authority = "http://localhost:5000";options.RequireHttpsMetadata = false;options.ClientId = "client";options.ClientSecret = "secret";options.SaveTokens = true;}); }

在 startup 的 Configure 中的 UseMvc 前添加 Authentication

app.UseAuthentication();

在 Program 的 CreateWebHostBuilder 中配置 Urls

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>WebHost.CreateDefaultBuilder(args).UseUrls("http://localhost:5001").UseStartup<Startup>();

客戶端設置為5001來啟動,然后服務端設置為5000

mvcCookieAuthSample 的 Program

public static IWebHost BuildWebHost(string[] args) =>WebHost.CreateDefaultBuilder(args).UseEnvironment("Development").UseUrls("http://localhost:5000").UseStartup<Startup>().Build();

修改服務端的 Config 配置跳轉地址

public static IEnumerable<Client> GetClients() {return new List<Client>{new Client(){ClientId = "client",AllowedGrantTypes = GrantTypes.Implicit,// 隱式模式ClientSecrets ={new Secret("secret".Sha256())},RedirectUris = { "http://localhost:5001/signin-oidc" },PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },//AllowedScopes = {"api"},AllowedScopes ={IdentityServerConstants.StandardScopes.Profile,IdentityServerConstants.StandardScopes.OpenId,}}}; }

客戶端的 Controller 打上 Authorize 標簽

[Authorize] public class HomeController : Controller

修改客戶端 launchSettings.json 中的 applicationUrl

"applicationUrl": "http://localhost:5001", "sslPort": 0

啟動服務端,客戶端,可以看到跳轉到登錄界面

登錄之后會跳轉到?http://localhost:5001/

在客戶端 About.cshtml 頁面顯示 identity 的 claims

@{ViewData["Title"] = "About"; } <h2>@ViewData["Title"]</h2> <h3>@ViewData["Message"]</h3>@*<p>Use this area to provide additional information.</p>*@<dl>@foreach (var claim in User.Claims){<dt>@claim.Type</dt><dt>@claim.Value</dt>} </dl>

啟動程序,跳轉之后,點擊 About 進入 About 頁面

主要返回了服務端 Config 中配置的信息

public static IEnumerable<IdentityResource> GetIdentityResources(){return new List<IdentityResource>{new IdentityResources.OpenId(),new IdentityResources.Profile(),new IdentityResources.Email(),};}

課程鏈接

http://video.jessetalk.cn/course/explore

相關文章

ASP.NET Core分布式項目實戰(oauth2 + oidc 實現 server部分)--學習筆記

ASP.NET Core分布式項目實戰(oauth2與open id connect 對比)--學習筆記

ASP.NET Core分布式項目實戰(詳解oauth2授權碼流程)--學習筆記

ASP.NET Core分布式項目實戰(oauth密碼模式identity server4實現)--學習筆記

ASP.NET&nbsp;Core分布式項目實戰(第三方ClientCredential模式調用)--學習筆記

ASP.NET Core分布式項目實戰(客戶端集成IdentityServer)--學習筆記

ASP.NET Core分布式項目實戰(業務介紹,架構設計,oAuth2,IdentityServer4)--學習筆記

ASP.NET Core分布式項目實戰(課程介紹,MVP,瀑布與敏捷)--學習筆記

ASP.NET Core快速入門 -- 學習筆記匯總

總結

以上是生活随笔為你收集整理的ASP.NET Core分布式项目实战(oauth2 + oidc 实现 client部分)--学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

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