{"id":534,"date":"2023-11-16T19:19:59","date_gmt":"2023-11-17T03:19:59","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/?p=534"},"modified":"2023-11-16T19:31:50","modified_gmt":"2023-11-17T03:31:50","slug":"net-8-%e4%b8%ad-identity%e7%9a%84%e6%96%b0%e5%a2%9e%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/net-8-%e4%b8%ad-identity%e7%9a%84%e6%96%b0%e5%a2%9e%e5%8a%9f%e8%83%bd\/","title":{"rendered":".NET 8 \u4e2d Identity\u7684\u65b0\u589e\u529f\u80fd"},"content":{"rendered":"<p><span style=\"font-size: 12pt; font-family: arial, helvetica, sans-serif;\"><em> \u672c\u7bc7\u7ffb\u8bd1\u4e8eJeremy Likness\u7684<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-with-identity-in-dotnet-8\/\">What&#8217;s new with identity in .NET 8 &#8211; .NET Blog (microsoft.com)<\/a><\/em><\/span><\/p>\n<h4><strong>.NET 8 \u4e2dIdentity\u7684\u65b0\u589e\u529f\u80fd<\/strong><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6211\u57282023 \u5e74 4 \u6708\u64b0\u5199\u7684\u6709\u5173 ASP.NET Core<\/span> \u56e2\u961f\u81f4\u529b\u4e8e<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/improvements-auth-identity-aspnetcore-8\/\">\u6539\u8fdb<\/a><span style=\"font-family: arial, helvetica, sans-serif;\"><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/improvements-auth-identity-aspnetcore-8\/\"> .NET 8 \u4e2d\u7684\u8eab\u4efd\u9a8c\u8bc1\u3001\u6388\u6743\u548cIdentity\u7ba1\u7406<\/a>\u7684\u6587\u7ae0\u4e2d\u63d0\u51fa\u7684\u8ba1\u5212\u5305\u62ec\u4e09\u4e2a\u5173\u952e\u6210\u679c\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u7528\u4e8e\u7b80\u5316\u767b\u5f55\u548cIdentity\u7ba1\u7406\u7684\u65b0API\uff0c\u9002\u7528\u4e8e\u50cf Single Page Apps (SPA) \u548cBlazor WebAssembly\u8fd9\u6837\u7684\u5ba2\u6237\u7aef\u7a0b\u5e8f\u3002<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u5728NET Core Identity \u4e2d\u4e3a\u65e0\u6cd5\u4f7f\u7528 cookie \u7684\u5ba2\u6237\u7aef\u542f\u7528\u57fa\u4e8e\u4ee4\u724c\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u3002<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u6587\u6863\u7684\u6539\u8fdb\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u8fd9\u4e09\u4e2a\u6210\u679c\u90fd\u5c06\u968f .NET 8 \u4e00\u8d77\u53d1\u5e03\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4e3a Blazor Web \u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u4e86\u65b0\u7684Identity UI\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e\u65b0\u7684\u6e32\u67d3\u6a21\u5f0f\uff08\u670d\u52a1\u5668\u548c WebAssembly \uff09\u914d\u5408\u4f7f\u7528\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b .NET 8 \u4e2d\u7684\u65b0\u53d8\u5316\u6240\u652f\u6301\u7684\u4e00\u4e9b\u573a\u666f\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u7bc7\u535a\u6587\u4e2d\u4ecb\u7ecd\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u786e\u4fdd\u7b80\u5355 Web API \u540e\u7aef\u7684\u5b89\u5168<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u4f7f\u7528\u65b0\u7684Blazor Identity UI<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u6dfb\u52a0\u5916\u90e8\u767b\u5f55\uff08\u4f8b\u5982 Google \u6216 Facebook\uff09<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u4f7f\u7528\u5185\u7f6e\u529f\u80fd\u548c\u7ec4\u4ef6\u4fdd\u62a4Blazor WebAssembly \u5e94\u7528\u7a0b\u5e8f<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u5bf9\u4e0d\u80fd\u4f7f\u7528 cookie \u7684\u5ba2\u6237\u7aef\u4f7f\u7528\u4ee4\u724c<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u4f7f\u7528\u65b0Identity\u529f\u80fd\u7684\u6700\u7b80\u5355\u573a\u666f\u3002<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\"><strong>\u57fa\u7840<\/strong><strong> Web API <\/strong><strong>\u540e\u7aef<\/strong><\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u4f7f\u7528\u65b0\u6388\u6743\u7684\u4e00\u79cd\u7b80\u5355\u65b9\u6cd5\u662f\u5728\u57fa\u7840 Web API \u5e94\u7528\u7a0b\u5e8f\u4e2d\u542f\u7528\u5b83\u3002\u540c\u6837\u7684\u5e94\u7528\u7a0b\u5e8f\u8fd8\u53ef\u4ee5\u7528\u4f5c Blazor WebAssembly\u3001Angular\u3001React \u548c\u5176\u4ed6\u5355\u9875 Web \u5e94\u7528\u7a0b\u5e8f (SPA) \u7684\u540e\u7aef\u3002\u5047\u8bbe\u60a8\u4ece .NET 8 \u4e2d\u5305\u542b OpenAPI \u7684 ASP.NET Core Web API \u9879\u76ee\u5165\u624b\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u51e0\u4e2a\u6b65\u9aa4\u6dfb\u52a0\u8eab\u4efd\u9a8c\u8bc1\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Identity\u662f\u201c\u53ef\u9009\u529f\u80fd\u201d\uff0c\u56e0\u6b64\u9700\u8981\u6dfb\u52a0\u4e00\u4e9b\u5305\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u652f\u6301 EF Core \u96c6\u6210\u7684\u5305\uff1aAspNetCore.Identity.EntityFrameworkCore<\/span><\/li>\n<li><span style=\"font-family: arial, helvetica, sans-serif;\">\u60a8\u8981\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7684\u5305\uff0c\u4f8b\u5982 EntityFrameworkCore.SqlServer\uff08\u672c\u4f8b\u4e2d\u6211\u4eec\u5c06\u4f7f\u7528\u5185\u5b58\u6570\u636e\u5e93\uff09<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u60a8\u53ef\u4ee5\u4f7f\u7528 NuGet \u5305\u7ba1\u7406\u5668\u6216\u547d\u4ee4\u884c\u6dfb\u52a0\u8fd9\u4e9b\u5305\u3002\u5982\u679c\u60a8\u8981\u4f7f\u7528\u547d\u4ee4\u884c\u6dfb\u52a0\u5305\uff0c\u8bf7\u5bfc\u822a\u5230\u9879\u76ee\u6587\u4ef6\u5939\u5e76\u8fd0\u884c\u4ee5\u4e0b dotnet \u547d\u4ee4\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore\r\ndotnet add package Microsoft.EntityFrameworkCore.InMemory<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Identity \u5141\u8bb8\u60a8\u81ea\u5b9a\u4e49\u7528\u6237\u4fe1\u606f\u548c\u7528\u6237\u6570\u636e\u5e93\uff0c\u4ee5\u9632\u60a8\u7684\u9700\u6c42\u8d85\u51fa .NET Core \u6846\u67b6\u4e2d\u63d0\u4f9b\u7684\u8303\u56f4\u3002\u5bf9\u4e8e\u6211\u4eec\u7684\u57fa\u672c\u793a\u4f8b\uff0c\u6211\u4eec\u5c06\u53ea\u4f7f\u7528\u9ed8\u8ba4\u7684\u7528\u6237\u4fe1\u606f\u548c\u6570\u636e\u5e93\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a MyUser \u7684\u65b0\u7c7b\uff0c\u8be5\u7c7b\u7ee7\u627f\u81ea IdentityUser\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">class MyUser : IdentityUser {}<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a AppDbContext \u7684\u65b0\u7c7b\uff0c\u8be5\u7c7b\u7ee7\u627f\u81ea IdentityDbContext&lt;MyUser&gt;\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">class AppDbContext(DbContextOptions&lt;AppDbContext&gt; options) :\r\n      IdentityDbContext&lt;MyUser&gt;(options)\r\n        {\r\n        }<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u63d0\u4f9b\u7279\u6b8a\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u73af\u5883\u914d\u7f6e\u6570\u636e\u5e93\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u5982\u679c\u60a8\u8981\u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684Identity\uff0c\u8bf7\u6253\u5f00 Program.cs \u6587\u4ef6\u3002\u914d\u7f6eIdentity\u4ee5\u4f7f\u7528\u57fa\u4e8e cookie \u7684\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5e76\u901a\u8fc7\u5728\u8c03\u7528 WebApplication.CreateBuilder(args) \u540e\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u6765\u542f\u7528\u6388\u6743\u68c0\u67e5\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">builder.Services.AddAuthentication(IdentityConstants.ApplicationScheme)\u00a0\u00a0\u00a0 \r\n    .AddIdentityCookies();\r\nbuilder.Services.AddAuthorizationBuilder();<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u914d\u7f6e EF Core \u6570\u636e\u5e93\u3002\u8fd9\u91cc\u6211\u4eec\u5c06\u4f7f\u7528\u5185\u5b58\u6570\u636e\u5e93\u5e76\u5c06\u5176\u547d\u540d\u4e3a\u201cAppDb\u201d\u3002 \u8fd9\u4ec5\u7528\u4e8e\u6f14\u793a\uff0c\u56e0\u6b64\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u91cd\u65b0\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u5e76\u6d4b\u8bd5\u6ce8\u518c\u548c\u767b\u5f55\u6d41\u7a0b\uff08\u6bcf\u6b21\u8fd0\u884c\u90fd\u5c06\u4ece\u65b0\u6570\u636e\u5e93\u5f00\u59cb\uff09\u3002\u66f4\u6539\u4e3a SQLite \u5c06\u5728\u4f1a\u8bdd\u4e4b\u95f4\u8282\u7701\u7528\u6237\uff0c\u4f46\u9700\u8981\u901a\u8fc7<a href=\"https:\/\/learn.microsoft.com\/en-us\/ef\/core\/managing-schemas\/migrations\/?tabs=dotnet-core-cli\">\u8fc1\u79fb<\/a>\u6b63\u786e\u521b\u5efa\u6570\u636e\u5e93\uff0c\u5982 <a href=\"https:\/\/learn.microsoft.com\/en-us\/ef\/core\/get-started\/overview\/first-app?tabs=netcore-cli\">EF Core \u5165\u95e8\u6559\u7a0b<\/a>\u4e2d\u6240\u793a\u3002\u60a8\u53ef\u4ee5\u5c06\u5176\u4ed6\u5173\u7cfb\u63d0\u4f9b\u7a0b\u5e8f\uff08\u4f8b\u5982 SQL Server\uff09\u7528\u4e8e\u60a8\u7684\u751f\u4ea7\u4ee3\u7801\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">builder.Services.AddDbContext&lt;AppDbContext&gt;(\r\n    options =&gt; options.UseInMemoryDatabase(\"AppDb\"));<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u914d\u7f6eIdentity\u4ee5\u4f7f\u7528 EF Core \u6570\u636e\u5e93\u5e76\u516c\u5f00Identity\u7aef\u70b9\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">builder.Services.AddIdentityCore&lt;MyUser&gt;()\r\n    .AddEntityFrameworkStores&lt;AppDbContext&gt;()\r\n    .AddApiEndpoints();<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6620\u5c04Identity\u7aef\u70b9\u7684\u8def\u7531\u3002 \u6b64\u4ee3\u7801\u5e94\u653e\u7f6e\u5728\u8c03\u7528 builder.Build() \u4e4b\u540e\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">app.MapIdentityApi&lt;MyUser&gt;();<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u8be5\u5e94\u7528\u7a0b\u5e8f\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u529f\u80fd\u5c31\u51c6\u5907\u597d\u4e86\uff01\u5982\u679c\u60a8\u60f3\u8981\u4fdd\u62a4\u7aef\u70b9\uff0c\u8bf7\u4f7f\u7528\u5b9a\u4e49auth\u8def\u7531\u7684 .RequireAuthentication() \u6269\u5c55\u65b9\u6cd5\u3002\u5982\u679c\u60a8\u4f7f\u7528\u57fa\u4e8e\u63a7\u5236\u5668\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5219\u53ef\u4ee5\u7ed9 [Authorize] controller\u6216action\u6dfb\u52a0 [Authorize]\u5c5e\u6027 \u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u5982\u679c\u60a8\u8981\u6d4b\u8bd5\u5e94\u7528\u7a0b\u5e8f\uff0c\u70b9\u51fb\u542f\u52a8\uff0c\u7136\u540e\u5bfc\u822a\u5230 Swagger UI\u3002 \u5c55\u5f00\u53d7\u4fdd\u62a4\u7684\u7aef\u70b9\uff0c\u9009\u62e9\u201c<strong>try it out<\/strong>\u201d\uff0c\u7136\u540e\u9009\u62e9\u201c<strong>execute<\/strong>\u201d\u3002 \u7aef\u70b9\u4f1a\u62a5 404\u00a0&#8211;\u00a0not\u00a0found\uff0c\u8fd9\u6bd4\u62a5 401\u00a0&#8211;\u00a0not\u00a0authorized\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u900f\u9732\u7aef\u70b9\u5b58\u5728\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-with-identity-in-dotnet-8\/nosoupforyou.png\" alt=\"Swagger UI with 404\" \/><\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u73b0\u5728\u5c55\u5f00 \/register \u5e76\u586b\u5199\u60a8\u7684\u51ed\u636e\u3002\u5982\u679c\u60a8\u8f93\u5165\u65e0\u6548\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u6216\u9519\u8bef\u7684\u5bc6\u7801\uff0c\u7ed3\u679c\u5c06\u5305\u542b\u9a8c\u8bc1\u9519\u8bef\u3002<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-with-identity-in-dotnet-8\/badpassword.png\" \/><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u672c\u793a\u4f8b\u4e2d\u7684errors \u4ee5<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/web-api\/handle-errors?view=aspnetcore-8.0#validation-failure-error-response\">ProblemDetails<\/a> \u683c\u5f0f\u8fd4\u56de\uff0c\u4ee5\u4fbf\u60a8\u7684\u5ba2\u6237\u7aef\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790\u5b83\u4eec\u5e76\u6839\u636e\u9700\u8981\u663e\u793a\u9a8c\u8bc1\u9519\u8bef\u3002\u6211\u5c06\u5728\u72ec\u7acb\u7684 Blazor WebAssembly \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5c55\u793a\u4e00\u4e2a\u793a\u4f8b\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6210\u529f\u6ce8\u518c\u4f1a\u6709 200 &#8211; OK\u7684\u8fd4\u56de \u3002\u60a8\u73b0\u5728\u53ef\u4ee5\u5c55\u5f00 \/login \u5e76\u8f93\u5165\u76f8\u540c\u7684\u51ed\u636e\u3002 \u8bf7\u6ce8\u610f\uff0c\u672c\u793a\u4f8b\u4e0d\u9700\u8981\u5176\u4ed6\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06\u5176\u5220\u9664\u3002 \u8bf7\u52a1\u5fc5\u5c06 useCookies \u8bbe\u7f6e\u4e3a true\u3002 \u6210\u529f\u767b\u5f55\u4f1a\u4ea7\u6709 200 &#8211; OK \u7684\u8fd4\u56de\uff0c\u8fd4\u56de\u7684header\u4e2d\u5305\u542b cookie\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u73b0\u5728\u60a8\u53ef\u4ee5\u91cd\u65b0\u8fd0\u884c\u53d7\u4fdd\u62a4\u7684\u7aef\u70b9\uff0c\u5b83\u5c06\u8fd4\u56de\u6709\u6548\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a\u57fa\u4e8e cookie \u7684\u8eab\u4efd\u9a8c\u8bc1\u5df2\u7ecf\u5b89\u5168\u5730\u5185\u7f6e\u4e8e\u60a8\u7684\u6d4f\u89c8\u5668\u4e2d\u5e76\u4e14\u76ee\u524d\u5b83\u201c\u5de5\u4f5c\u6b63\u5e38\u201d\u3002 \u60a8\u521a\u521a\u901a\u8fc7Identity\u4fdd\u62a4\u4e86\u60a8\u7684\u7b2c\u4e00\u4e2a\u7aef\u70b9\uff01<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u67d0\u4e9b Web \u5ba2\u6237\u7aef\u53ef\u80fd\u4e0d\u4f1a\u5728header\u4e2d\u5305\u542b cookie\u3002\u5982\u679c\u60a8\u4f7f\u7528\u5de5\u5177\u6765\u6d4b\u8bd5API\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728\u8bbe\u7f6e\u4e2d\u542f\u7528cookie\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cJavaScript fetch API \u4e0d\u5305\u542b cookie\u3002 \u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u51ed\u636e\u8bbe\u7f6e\u4e3a\u9009\u9879\u4e2d\u5305\u542b\u7684\u503c\u6765\u542f\u7528\u5b83\u4eec\u3002 \u540c\u6837\uff0c\u5728 Blazor WebAssembly \u5e94\u7528\u7a0b\u5e8f\u4e2d\u8fd0\u884c\u7684 HttpClient \u9700\u8981 HttpRequestMessage \u5305\u542b\u51ed\u636e\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">request.SetBrowserRequestCredential(BrowserRequestCredentials.Include);<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u8fdb\u5165 Blazor Web \u5e94\u7528\u7a0b\u5e8f\u3002<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\"><strong>Blazor Identity UI<\/strong><\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6211\u4eec\u56e2\u961f\u7684\u4e00\u4e2a\u5ef6\u4f38\u76ee\u6807\u8fdc\u5927\u76ee\u6807\u662f\u5728 Blazor \u4e2d\u5b9e\u73b0Identity UI\uff0c\u5176\u4e2d\u5305\u62ec\u6ce8\u518c\u3001\u767b\u5f55\u548c\u914d\u7f6e\u591a\u91cd\u8eab\u4efd\u9a8c\u8bc1\u7684\u9009\u9879\u3002 \u5f53\u60a8\u9009\u62e9\u201cIndividual accounts\u201d\u9009\u9879\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u65f6\uff0cUI \u4f1a\u5185\u7f6e\u5230\u6a21\u677f\u4e2d\u3002 \u4e0e\u4ee5\u524d\u7248\u672c\u7684Identity UI \u4e0d\u540c\u7684\u662f\uff0c\u9664\u975e\u60a8\u60f3\u8981\u81ea\u5b9a\u4e49\u5b83\uff0c\u5426\u5219\u5b83\u662f\u9690\u85cf\u7684\uff0c\u8be5\u6a21\u677f\u4f1a\u751f\u6210\u6240\u6709\u6e90\u4ee3\u7801\uff0c\u4ee5\u4fbf\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002 \u65b0\u7248\u672c\u662f\u4f7f\u7528 Razor \u7ec4\u4ef6\u6784\u5efa\u7684\uff0c\u53ef\u4e0e\u670d\u52a1\u5668\u7aef\u548c WebAssembly Blazor \u5e94\u7528\u7a0b\u5e8f\u914d\u5408\u4f7f\u7528\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-with-identity-in-dotnet-8\/blazorlogin.png\" alt=\"Blazor login page\" \/><\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u65b0\u7684 Blazor Web \u6a21\u578b\u5141\u8bb8\u60a8\u914d\u7f6e UI\u5728\u670d\u52a1\u5668\u7aef\u5448\u73b0\u6216\u8005\u5728 WebAssembly \u4e2d\u8fd0\u884c\u7684\u5ba2\u6237\u7aef\u5448\u73b0\u3002\u5f53\u60a8\u9009\u62e9WebAssembly\u6a21\u5f0f\u65f6\uff0c\u670d\u52a1\u5668\u4ecd\u5c06\u5904\u7406\u6240\u6709\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u8bf7\u6c42\u3002\u5b83\u8fd8\u5c06\u4e3a\u8ddf\u8e2a\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u7684 AuthenticationStateProvider \u81ea\u5b9a\u4e49\u5b9e\u73b0\u751f\u6210\u4ee3\u7801\u3002\u63d0\u4f9b\u7a0b\u5e8f\u4f7f\u7528 <a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/blazor\/components\/prerender?view=aspnetcore-8.0#persist-prerendered-state\">PersistentComponentState<\/a> \u7c7b\u6765\u9884\u5448\u73b0\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u5e76\u5c06\u5176\u4fdd\u7559\u5230\u9875\u9762\u3002\u5ba2\u6237\u7aef WebAssembly \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684 PersistentAuthenticationStateProvider \u4f7f\u7528\u8be5\u7ec4\u4ef6\u6765\u540c\u6b65\u670d\u52a1\u5668\u548c\u6d4f\u89c8\u5668\u4e4b\u95f4\u7684\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u3002 \u5f53\u4e0e\u81ea\u52a8\u4ea4\u4e92\u4e00\u8d77\u8fd0\u884c\u65f6\uff0c\u72b6\u6001\u63d0\u4f9b\u7a0b\u5e8f\u4e5f\u53ef\u4ee5\u88ab\u547d\u540d\u4e3a PersistingRevalidatingAuthenticationStateProvider \u6216\u7528\u4e8e\u670d\u52a1\u5668\u4ea4\u4e92\u7684 IdentityRevalidatingAuthenticationStateProvider\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u867d\u7136\u672c\u535a\u6587\u4e2d\u7684\u793a\u4f8b\u4fa7\u91cd\u4e8e\u7b80\u5355\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u767b\u5f55\u573a\u666f\uff0c\u4f46 ASP.NET Identity \u652f\u6301\u57fa\u4e8e\u7535\u5b50\u90ae\u4ef6\u7684\u4ea4\u4e92\uff0c\u4f8b\u5982<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/security\/authentication\/accconfirm?view=aspnetcore-8.0&amp;tabs=visual-studio\">\u5e10\u6237\u786e\u8ba4\u548c\u5bc6\u7801\u6062\u590d<\/a>\u3002\u8fd8\u53ef\u4ee5\u914d\u7f6e<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/security\/authentication\/mfa?view=aspnetcore-8.0\">\u591a\u91cd\u8eab\u4efd\u9a8c\u8bc1<\/a>\u3002\u6240\u6709\u8fd9\u4e9b\u529f\u80fd\u7684\u7ec4\u4ef6\u90fd\u5305\u542b\u5728 UI \u4e2d\u3002<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\"><strong>\u6dfb\u52a0\u5916\u90e8\u767b\u5f55<\/strong><\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6211\u4eec\u7ecf\u5e38\u88ab\u95ee\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u4f55\u5c06\u793e\u4ea4\u7f51\u7ad9\u7684\u5916\u90e8\u767b\u5f55\u4e0e ASP.NET Core Identity \u96c6\u6210\u3002\u73b0\u5728\uff0c\u4ece Blazor Web \u5e94\u7528\u9ed8\u8ba4\u9879\u76ee\u5f00\u59cb\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u6dfb\u52a0\u5916\u90e8\u767b\u5f55\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u793e\u4ea4\u7f51\u7ad9\u4e0a\u6ce8\u518c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002 \u4f8b\u5982\uff0c\u8981\u6dfb\u52a0 Twitter \u767b\u5f55\uff0c\u8bf7\u8f6c\u5230 <a href=\"https:\/\/developer.twitter.com\/portal\/dashboard\">Twitter \u5f00\u53d1\u4eba\u5458\u95e8\u6237<\/a>\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u5e94\u7528\u7a0b\u5e8f\u3002\u60a8\u9700\u8981\u63d0\u4f9b\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\u624d\u80fd\u83b7\u53d6\u60a8\u7684\u5ba2\u6237\u51ed\u636e\u3002 \u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u540e\uff0c\u8bf7\u5bfc\u822a\u81f3\u5e94\u7528\u7a0b\u5e8f\u8bbe\u7f6e\u5e76\u5355\u51fb\u8eab\u4efd\u9a8c\u8bc1\u4e0a\u7684\u201cedit\u201d\u3002\u7136\u540e\u5c06\u5e94\u7528\u7a0b\u5e8f\u7c7b\u578b\u6307\u5b9a\u4e3a\u201cnative app\u201d\uff0c\u4ee5\u4fbf\u6d41\u7a0b\u6b63\u5e38\u5de5\u4f5c\uff0c\u968f\u540e\u542f\u7528\u201crequest email from users\u201d\u9009\u9879\u3002\u60a8\u9700\u8981\u63d0\u4f9b\u56de\u8c03 URL\u3002\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 https:\/\/localhost:5001\/signin-twitter\uff0c\u8fd9\u662f Blazor Web \u5e94\u7528\u6a21\u677f\u7684\u9ed8\u8ba4\u56de\u8c03 URL\u3002 \u60a8\u53ef\u4ee5\u66f4\u6539\u6b64\u8bbe\u7f6e\u4ee5\u5339\u914d\u60a8\u5e94\u7528\u7a0b\u5e8f\u7684 URL\uff08\u4f8b\u5982\u5c06\u60a8\u81ea\u5df1\u7684\u7aef\u53e3\u66ff\u6362\u4e3a5001\uff09.\u53e6\u5916\u8bf7\u6ce8\u610f API \u5bc6\u94a5\u548c\u5bc6\u7801\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u63a5\u4e0b\u6765\uff0c\u5c06\u9002\u5f53\u7684\u8eab\u4efd\u9a8c\u8bc1\u5305\u6dfb\u52a0\u5230\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u6709\u4e00\u4e2a\u793e\u533a\u7ef4\u62a4\u7684 <a href=\"https:\/\/github.com\/aspnet-contrib\/AspNet.Security.OAuth.Providers\">ASP.NET Core OAuth 2.0 \u793e\u4ea4\u8eab\u4efd\u9a8c\u8bc1\u63d0\u4f9b\u7a0b\u5e8f\u5217\u8868<\/a>\uff0c\u5176\u4e2d\u6709\u8bb8\u591a\u9009\u9879\u53ef\u4f9b\u9009\u62e9\u3002 \u60a8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6df7\u5408\u591a\u4e2a\u5916\u90e8\u767b\u5f55\u3002 \u5bf9\u4e8e Twitter\uff0c\u6211\u5c06\u6dfb\u52a0 AspNet.Security.OAuth.Twitter \u5305\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u4ece\u670d\u52a1\u5668\u9879\u76ee\u6839\u76ee\u5f55\u4e2d\u7684\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b58\u50a8\u60a8\u7684 API \u5bc6\u94a5\uff08client ID\uff09\u548csecret\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">dotnet user-secrets set \"Twitter:ApiKey\" \"&lt;your-api-key&gt;\"\r\ndotnet user-secrets set \"TWitter:ApiSecret\" \"&lt;your-api-secret&gt;\"<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6700\u540e\uff0c\u5728 Program.cs \u4e2d\u914d\u7f6e\u767b\u5f55\uff0c\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u66ff\u6362\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">builder.Services.AddAuthentication(IdentityConstants.ApplicationScheme)\r\n    .AddIdentityCookies();<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u4f7f\u7528\u8fd9\u4e2a\u4ee3\u7801\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">builder.Services.AddAuthentication(IdentityConstants.ApplicationScheme)\r\n    .AddTwitter(opt =&gt;\r\n        {\r\n            opt.ClientId = builder.Configuration[\"Twitter:ApiKey\"]!;\r\n            opt.ClientSecret = builder.Configuration[\"Twitter:ApiSecret\"]!;\r\n        })\r\n    .AddIdentityCookies();<\/code><\/span><\/pre>\n<blockquote><p><span style=\"font-family: arial, helvetica, sans-serif;\">Cookie \u662f\u5b9e\u73b0 ASP.NET Core Identity \u7684\u9996\u9009\u65b9\u5f0f\u5e76\u4e14\u662f\u6700\u5b89\u5168\u7684\u65b9\u6cd5\u3002\u5982\u679c\u6709\u9700\u8981\uff0c\u4ee4\u724c\uff08token\uff09\u4e5f\u662f\u652f\u6301\u7684\uff0c\u4f46\u662f\u9700\u8981\u914d\u7f6e IdentityConstants.BearerScheme\u3002 \u4ee4\u724c\u662f\u4e13\u6709\u7684\uff0c\u57fa\u4e8e\u4ee4\u724c\u7684\u6d41\u7a0b\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f\uff0c\u56e0\u6b64\u5b83\u4e0d\u5b9e\u73b0 OAuth 2.0 \u6216 OIDC \u6807\u51c6\u3002<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u8fd9\u6b21\u5f53\u60a8\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u767b\u5f55\u9875\u9762\u5c06\u81ea\u52a8\u68c0\u6d4b\u5916\u90e8\u767b\u5f55\u5e76\u63d0\u4f9b\u4e00\u4e2a\u6309\u94ae\u6765\u4f7f\u7528\u5b83\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-with-identity-in-dotnet-8\/externallogin.png\" alt=\"Blazor login page with Twitter\" \/><\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u5f53\u60a8\u767b\u5f55\u5e76\u6388\u6743\u8be5\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u60a8\u5c06\u88ab\u91cd\u5b9a\u5411\u56de\u6765\u5e76\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\"><strong>\u4fdd\u62a4<\/strong><strong> Blazor WebAssembly <\/strong><strong>\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168<\/strong><\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6dfb\u52a0\u65b0Identity API \u7684\u4e00\u4e2a\u4e3b\u8981\u52a8\u673a\u662f\u8ba9\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u4fdd\u62a4\u57fa\u4e8e\u6d4f\u89c8\u5668\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec Single Page Apps (SPA) \u548c Blazor WebAssembly\u3002\u65e0\u8bba\u60a8\u4f7f\u7528\u5185\u7f6eIdentity\u63d0\u4f9b\u7a0b\u5e8f\u3001\u81ea\u5b9a\u4e49\u767b\u5f55\u8fd8\u662f <a href=\"https:\/\/www.microsoft.com\/security\/business\/microsoft-entra\">Microsoft Entra<\/a> \u7b49\u57fa\u4e8e\u4e91\u7684\u670d\u52a1\uff0c\u6700\u7ec8\u7ed3\u679c\u90fd\u662f\u901a\u8fc7\u58f0\u660e\u548c\u89d2\u8272\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u6216\u672a\u7ecf\u8eab\u4efd\u9a8c\u8bc1\u7684Identity\u3002\u5728 Blazor \u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5411\u7ec4\u4ef6\u6216\u6258\u7ba1\u8be5\u7ec4\u4ef6\u7684\u9875\u9762\u6dfb\u52a0 [Authorize] \u5c5e\u6027\u6765\u4fdd\u62a4 razor \u7ec4\u4ef6\u3002\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728\u8def\u7531\u5b9a\u4e49\u4e2d\u6dfb\u52a0<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">.RequireAuthorization()\u6269\u5c55\u65b9\u6cd5\u6765\u4fdd\u62a4\u8def\u7531\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">Blazor \u793a\u4f8b\u5b58\u50a8\u5e93\u4e2d\u63d0\u4f9b\u4e86\u6b64\u793a\u4f8b\u7684\u5b8c\u6574\u6e90\u4ee3\u7801\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">AuthorizeView \u6807\u7b7e\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u65b9\u6cd5\u6765\u5904\u7406\u7528\u6237\u6709\u6743\u8bbf\u95ee\u7684\u5185\u5bb9\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7context\u5c5e\u6027\u8bbf\u95ee\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u3002\u53c2\u8003\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">&lt;p&gt;Welcome to my page!&lt;\/p&gt;\r\n&lt;AuthorizeView&gt;\r\n    &lt;Authorizing&gt;\r\n        &lt;div class=\"alert alert-info\"&gt;We're checking your credentials...&lt;\/div&gt;\r\n    &lt;\/Authorizing&gt;\r\n    &lt;Authorized&gt;\r\n        &lt;div class=\"alert alert-success\"&gt;You are authenticated @context.User.Identity?.Name&lt;\/div&gt;\r\n    &lt;\/Authorized&gt;\r\n    &lt;NotAuthorized&gt;\r\n        &lt;div class=\"alert alert-warning\"&gt;You are not authenticated!&lt;\/div&gt;\r\n    &lt;\/NotAuthorized&gt;\r\n&lt;\/AuthorizeView&gt;<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u95ee\u5019\u8bed\u5c06\u663e\u793a\u7ed9\u6240\u6709\u4eba\u3002\u5bf9\u4e8e Blazor WebAssembly\uff0c\u5f53\u5ba2\u6237\u7aef\u53ef\u80fd\u9700\u8981\u901a\u8fc7 API \u8c03\u7528\u8fdb\u884c\u5f02\u6b65\u8eab\u4efd\u9a8c\u8bc1\u65f6\uff0c\u5728\u67e5\u8be2\u548c\u89e3\u6790\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u65f6\u663e\u793a\u6388\u6743\u5185\u5bb9\u3002\u7136\u540e\uff0c\u6839\u636e\u60a8\u662f\u5426\u5df2\u901a\u8fc7\u8eab\u4efd\u9a8c\u8bc1\uff0c\u60a8\u5c06\u770b\u5230\u60a8\u7684\u59d3\u540d\u6216\u4e00\u6761\u8868\u660e\u60a8\u672a\u901a\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u6d88\u606f\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u4f7f\u7528AuthenticationStateProvider \u53ef\u4ee5\u77e5\u9053\u60a8\u662f\u5426\u901a\u8fc7\u4e86\u8eab\u4efd\u9a8c\u8bc1\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">App.razor \u9875\u9762\u88ab\u5c01\u88c5\u5728 CascadingAuthenticationState \u63d0\u4f9b\u7a0b\u5e8f\u4e2d\u3002 \u8be5\u63d0\u4f9b\u7a0b\u5e8f\u8d1f\u8d23\u8ddf\u8e2a\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u5e76\u5c06\u5176\u63d0\u4f9b\u7ed9\u5e94\u7528\u7a0b\u5e8f\u7684\u5176\u4f59\u90e8\u5206\u3002AuthenticationStateProvider \u88ab\u6ce8\u5165\u5230\u63d0\u4f9b\u7a0b\u5e8f\u4e2d\u5e76\u7528\u4e8e\u8ddf\u8e2a\u72b6\u6001\u3002AuthenticationStateProvider \u4e5f\u88ab\u6ce8\u5165\u5230 AuthorizeView \u7ec4\u4ef6\u4e2d\u3002\u5f53\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u63d0\u4f9b\u8005\u4f1a\u901a\u77e5 AuthorizeView \u7ec4\u4ef6\uff0c\u5185\u5bb9\u4e5f\u4f1a\u76f8\u5e94\u66f4\u65b0\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u9996\u5148\uff0c\u6211\u4eec\u8981\u786e\u4fdd API \u8c03\u7528\u6309\u9884\u671f\u4fdd\u5b58\u51ed\u636e\u3002\u4e3a\u6b64\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a CookieHandler \u7684\u5904\u7406\u7a0b\u5e8f\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">public class CookieHandler : DelegatingHandler\r\n{\r\n    protected override Task&lt;HttpResponseMessage&gt; SendAsync(\r\n        HttpRequestMessage request, CancellationToken cancellationToken)\r\n    {\r\n        request.SetBrowserRequestCredentials(BrowserRequestCredentials.Include);\r\n        return base.SendAsync(request, cancellationToken);\r\n    }\r\n}<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u5728 Program.cs \u4e2d\uff0c\u6211\u5c06\u5904\u7406\u7a0b\u5e8f\u6dfb\u52a0\u5230 HttpClient \u5e76\u4f7f\u7528\u5ba2\u6237\u7aef\u5de5\u5382\u6765\u914d\u7f6e\u7279\u6b8a\u5ba2\u6237\u7aef\u4ee5\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">builder.Services.AddTransient&lt;CookieHandler&gt;();\r\nbuilder.Services.AddHttpClient(\r\n    \"Auth\",\r\n    opt =&gt; opt.BaseAddress = new Uri(builder.Configuration[\"AuthUrl\"]!))\r\n    .AddHttpMessageHandler&lt;CookieHandler&gt;();<\/code><\/span><\/pre>\n<blockquote><p><span style=\"font-family: arial, helvetica, sans-serif;\">\u6ce8\u610f\uff1a\u8eab\u4efd\u9a8c\u8bc1\u7ec4\u4ef6\u662f\u53ef\u9009\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7Microsoft.AspNetCore.Components.WebAssembly.Authentication \u5305\u4f7f\u7528\u3002 \u5ba2\u6237\u7aef\u5de5\u5382\u548c\u6269\u5c55\u65b9\u6cd5\u6765\u81ea Microsoft.Extensions.Http\u3002<\/span><\/p><\/blockquote>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">AuthUrl \u662f\u516c\u5f00Identity API \u7684 ASP.NET Core \u670d\u52a1\u5668\u7684 URL\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u7ee7\u627f\u81ea AuthenticationStateProvider \u7684 CookieAuthenticationStateProvider \u5e76\u91cd\u5199 GetAuthenticationStateAsync \u65b9\u6cd5\u3002 \u4e3b\u8981\u903b\u8f91\u5982\u4e0b\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">var unauthenticated = new ClaimsPrincipal(new ClaimsIdentity());\r\nvar userResponse = await _httpClient.GetAsync(\"manage\/info\");\r\nif (userResponse.IsSuccessStatusCode) \r\n{\r\n    var userJson = await userResponse.Content.ReadAsStringAsync();\r\n    var userInfo = JsonSerializer.Deserialize&lt;UserInfo&gt;(userJson, jsonSerializerOptions);\r\n    if (userInfo != null)\r\n    {\r\n        var claims = new List&lt;Claim&gt;\r\n        {\r\n            new(ClaimTypes.Name, userInfo.Email),\r\n            new(ClaimTypes.Email, userInfo.Email)\r\n        };\r\n        var id = new ClaimsIdentity(claims, nameof(CookieAuthenticationStateProvider));\r\n        user = new ClaimsPrincipal(id);\r\n    }\r\n}\r\n\r\nreturn new AuthenticationState(user);<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u7528\u6237\u4fe1\u606f\u7aef\u70b9\u662f\u5b89\u5168\u7684\uff0c\u56e0\u6b64\u5982\u679c\u7528\u6237\u672a\u7ecf\u8eab\u4efd\u9a8c\u8bc1\uff0c\u8bf7\u6c42\u5c06\u5931\u8d25\uff0c\u5e76\u4e14\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u672a\u7ecf\u8eab\u4efd\u9a8c\u8bc1\u7684\u72b6\u6001\u3002\u5426\u5219\uff0c\u5b83\u4f1a\u6784\u5efa\u9002\u5f53\u7684Identity\u548c\u58f0\u660e\u5e76\u8fd4\u56de\u7ecf\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u72b6\u6001\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u5e94\u7528\u7a0b\u5e8f\u5982\u4f55\u77e5\u9053\u72b6\u6001\u4f55\u65f6\u53d1\u751f\u53d8\u5316\uff1f \u4ee5\u4e0b\u662f\u4f7f\u7528Identity API \u4ece Blazor WebAssembly \u8fdb\u884c\u767b\u5f55\u7684\u60c5\u51b5\uff1a<\/span><\/p>\n<pre class=\"prettyprint language-default\"><span style=\"font-family: arial, helvetica, sans-serif;\"><code class=\"language-default\">async Task&lt;AuthenticationState&gt; LoginAndGetAuthenticationState()\r\n{\r\n    var result = await _httpClient.PostAsJsonAsync(\r\n        \"login?useCookies=true\", new\r\n        {\r\n            email,\r\n            password\r\n        });\r\n\r\n        return await GetAuthenticationStateAsync();\r\n}\r\n\r\nNotifyAuthenticationStateChanged(LoginAndGetAuthenticationState());<\/code><\/span><\/pre>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u767b\u5f55\u6210\u529f\u540e\uff0c\u5c06\u8c03\u7528 AuthenticationStateProvider \u57fa\u7c7b\u4e0a\u7684<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">NotifyAuthenticationStateChanged \u65b9\u6cd5\u6765\u901a\u77e5\u63d0\u4f9b\u7a0b\u5e8f\u72b6\u6001\u5df2\u66f4\u6539\u3002\u5b83\u4f1a\u4f20\u9012\u65b0\u7684\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u8bf7\u6c42\u7684\u7ed3\u679c\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u9a8c\u8bc1 cookie \u662f\u5426\u5b58\u5728\u3002\u7136\u540e\uff0c\u63d0\u4f9b\u7a0b\u5e8f\u5c06\u66f4\u65b0 AuthorizeView \u7ec4\u4ef6\uff0c\u7528\u6237\u5c06\u770b\u5230\u7ecf\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u5185\u5bb9\u3002<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\"><strong>\u4ee4\u724c\uff08<\/strong><strong>Tokens<\/strong><strong>\uff09<\/strong><\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u5728\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u60a8\u7684\u5ba2\u6237\u7aef\u4e0d\u652f\u6301 cookie\uff0c\u767b\u5f55 API \u4f1a\u63d0\u4f9b\u4e00\u4e2a\u53c2\u6570\u6765\u8bf7\u6c42\u4ee4\u724c\u3002 \u4e00\u4e2a\u81ea\u5b9a\u4e49\u4ee4\u724c\uff08ASP.NET Core Identity\u5e73\u53f0\u4e13\u6709\u7684\u4ee4\u724c\uff09\uff0c\u53ef\u7528\u4e8e\u5bf9\u540e\u7eed\u8bf7\u6c42\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u4ee4\u724c\u4f5c\u4e3a\u627f\u8f7d\u4ee4\u724c\u5728Authorization\u6807\u5934\u4e2d\u4f20\u9012\u3002\u5176\u6b21\uff0cAPI\u8fd8\u63d0\u4f9b\u5237\u65b0\u4ee4\u724c\u3002\u8fd9\u5141\u8bb8\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u5728\u65e7\u4ee4\u724c\u8fc7\u671f\u65f6\u8bf7\u6c42\u65b0\u4ee4\u724c\uff0c\u800c\u65e0\u9700\u5f3a\u5236\u7528\u6237\u518d\u6b21\u767b\u5f55\u3002\u8fd9\u4e9b\u4ee4\u724c\u4e0d\u662f\u6807\u51c6 JSON Web \u4ee4\u724c (JWT)\u3002\u8fd9\u662f\u6709\u610f\u4e3a\u4e4b\u7684\uff0c\u56e0\u4e3a\u5185\u7f6eIdentity\u4e3b\u8981\u7528\u4e8e\u7b80\u5355\u7684\u573a\u666f\u3002\u4ee4\u724c\u9009\u9879\u5e76\u4e0d\u662f\u529f\u80fd\u9f50\u5168\u7684Identity\u670d\u52a1\u63d0\u4f9b\u65b9\u6216\u4ee4\u724c\u670d\u52a1\u5668\uff0c\u800c\u53ea\u662f\u4e3a\u65e0\u6cd5\u4f7f\u7528 cookie \u7684\u5ba2\u6237\u7aef\u63d0\u4f9b\u7684\u66ff\u4ee3 cookie \u9009\u9879\u7684\u65b9\u6848\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u5982\u679c\u60a8\u4e0d\u786e\u5b9a\u662f\u5426\u9700\u8981\u4ee4\u724c\u670d\u52a1\u5668\uff0c\u60a8\u53ef\u4ee5\u9605\u8bfb\u6587\u6863\u6765\u5e2e\u52a9\u60a8<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/security\/how-to-choose-identity-solution?view=aspnetcore-7.0\">\u9009\u62e9\u6b63\u786e\u7684 ASP.NET Core Identity\u89e3\u51b3\u65b9\u6848<\/a>\u3002\u8bf7\u9605\u8bfb\u6211\u4eec\u7684 <a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/security\/identity-management-solutions?view=aspnetcore-7.0\">ASP.NET Core Identity\u7ba1\u7406\u89e3\u51b3\u65b9\u6848\u5217\u8868<\/a>\u4ee5\u4f7f\u7528\u66f4\u5148\u8fdb\u7684Identity\u89e3\u51b3\u65b9\u6848\u3002<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\"><strong>\u6587\u6863\u548c\u793a\u4f8b<\/strong><\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u7b2c\u4e09\u4e2a\u6210\u679c\u662f\u6587\u6863\u548c\u793a\u4f8b\u6837\u672c\u3002\u6211\u4eec\u5f15\u5165\u4e86\u65b0\u7684\u6587\u6863\uff0c\u5e76\u5c06\u5728 .NET 8 \u53d1\u5e03\u65f6\u6dfb\u52a0\u65b0\u7684\u6587\u7ae0\u548c\u793a\u4f8b\u3002\u8bf7\u5173\u6ce8<a href=\"https:\/\/github.com\/dotnet\/AspNetCore.Docs\/issues\/29452\">\u95ee\u9898 #29452 \u2013 .NET 8 \u4e2d\u7684Identity\u6587\u6863\u548c\u793a\u4f8b<\/a>\u6765\u8ddf\u8e2a\u8fdb\u5ea6\u3002\u8bf7\u5728\u8fd9\u4e2a\u95ee\u9898\u4e0b\u9762\u8be2\u95ee\u60a8\u6b63\u5728\u5bfb\u627e\u7684\u5176\u4ed6\u6587\u6863\u6216\u793a\u4f8b\u3002\u60a8\u8fd8\u53ef\u4ee5\u94fe\u63a5\u5230\u5404\u79cd\u6587\u6863\u7684\u5177\u4f53\u95ee\u9898\u5e76\u5728\u90a3\u91cc\u63d0\u4f9b\u53cd\u9988\u3002<\/span><\/p>\n<h4><span style=\"font-family: arial, helvetica, sans-serif;\"><strong>\u7ed3\u8bba<\/strong><\/span><\/h4>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">.NET 8 \u4e2d\u7684\u65b0Identity\u529f\u80fd\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u4fdd\u62a4\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u3002\u5982\u679c\u60a8\u7684\u9700\u6c42\u5f88\u7b80\u5355\uff0c\u60a8\u73b0\u5728\u53ea\u9700\u901a\u8fc7\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5411\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0Identity\u9a8c\u8bc1\u548c\u6388\u6743\u3002\u65b0\u7684 API \u53ef\u4ee5\u901a\u8fc7\u57fa\u4e8e cookie \u7684Identity\u9a8c\u8bc1\u548c\u6388\u6743\u6765\u4fdd\u62a4\u60a8\u7684 Web API \u7aef\u70b9\u3002\u5bf9\u4e8e\u4e0d\u80fd\u4f7f\u7528 cookie \u7684\u5ba2\u6237\u7aef\uff0c\u8fd8\u6709\u4e00\u4e2a\u57fa\u4e8e\u4ee4\u724c\u7684\u9009\u9879\u3002<\/span><\/p>\n<p><span style=\"font-family: arial, helvetica, sans-serif;\">\u60a8\u53ef\u4ee5\u5728 <a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/release-notes\/aspnetcore-8.0?view=aspnetcore-7.0#authentication-and-authorization\">ASP.NET Core \u6587\u6863<\/a>\u4e2d\u4e86\u89e3\u6709\u5173\u65b0Identity\u529f\u80fd\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u7ffb\u8bd1\u4e8eJeremy Likness\u7684What&#8217;s new with identity in .NET 8 &#8211; .NET Blog (microsoft.com) .NET 8 \u4e2dIdentity\u7684\u65b0\u589e\u529f\u80fd \u6211\u57282023 \u5e74 4 \u6708\u64b0\u5199\u7684\u6709\u5173 ASP.NET Core \u56e2\u961f\u81f4\u529b\u4e8e\u6539\u8fdb .NET 8 \u4e2d\u7684\u8eab\u4efd\u9a8c\u8bc1\u3001\u6388\u6743\u548cIdentity\u7ba1\u7406\u7684\u6587\u7ae0\u4e2d\u63d0\u51fa\u7684\u8ba1\u5212\u5305\u62ec\u4e09\u4e2a\u5173\u952e\u6210\u679c\uff1a \u7528\u4e8e\u7b80\u5316\u767b\u5f55\u548cIdentity\u7ba1\u7406\u7684\u65b0API\uff0c\u9002\u7528\u4e8e\u50cf Single Page Apps (SPA) \u548cBlazor WebAssembly\u8fd9\u6837\u7684\u5ba2\u6237\u7aef\u7a0b\u5e8f\u3002 \u5728NET Core Identity \u4e2d\u4e3a\u65e0\u6cd5\u4f7f\u7528 cookie \u7684\u5ba2\u6237\u7aef\u542f\u7528\u57fa\u4e8e\u4ee4\u724c\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u3002 \u6587\u6863\u7684\u6539\u8fdb\u3002 \u8fd9\u4e09\u4e2a\u6210\u679c\u90fd\u5c06\u968f .NET 8 \u4e00\u8d77\u53d1\u5e03\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4e3a Blazor Web \u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u4e86\u65b0\u7684Identity UI\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e\u65b0\u7684\u6e32\u67d3\u6a21\u5f0f\uff08\u670d\u52a1\u5668\u548c WebAssembly \uff09\u914d\u5408\u4f7f\u7528\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b .NET 8 \u4e2d\u7684\u65b0\u53d8\u5316\u6240\u652f\u6301\u7684\u4e00\u4e9b\u573a\u666f\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u7bc7\u535a\u6587\u4e2d\u4ecb\u7ecd\uff1a \u786e\u4fdd\u7b80\u5355 Web [&hellip;]<\/p>\n","protected":false},"author":99475,"featured_media":547,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,23,13],"class_list":["post-534","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet-ch","tag-net","tag-net-core","tag-asp-net-core"],"acf":[],"blog_post_summary":"<p>\u672c\u7bc7\u7ffb\u8bd1\u4e8eJeremy Likness\u7684What&#8217;s new with identity in .NET 8 &#8211; .NET Blog (microsoft.com) .NET 8 \u4e2dIdentity\u7684\u65b0\u589e\u529f\u80fd \u6211\u57282023 \u5e74 4 \u6708\u64b0\u5199\u7684\u6709\u5173 ASP.NET Core \u56e2\u961f\u81f4\u529b\u4e8e\u6539\u8fdb .NET 8 \u4e2d\u7684\u8eab\u4efd\u9a8c\u8bc1\u3001\u6388\u6743\u548cIdentity\u7ba1\u7406\u7684\u6587\u7ae0\u4e2d\u63d0\u51fa\u7684\u8ba1\u5212\u5305\u62ec\u4e09\u4e2a\u5173\u952e\u6210\u679c\uff1a \u7528\u4e8e\u7b80\u5316\u767b\u5f55\u548cIdentity\u7ba1\u7406\u7684\u65b0API\uff0c\u9002\u7528\u4e8e\u50cf Single Page Apps (SPA) \u548cBlazor WebAssembly\u8fd9\u6837\u7684\u5ba2\u6237\u7aef\u7a0b\u5e8f\u3002 \u5728NET Core Identity \u4e2d\u4e3a\u65e0\u6cd5\u4f7f\u7528 cookie \u7684\u5ba2\u6237\u7aef\u542f\u7528\u57fa\u4e8e\u4ee4\u724c\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u3002 \u6587\u6863\u7684\u6539\u8fdb\u3002 \u8fd9\u4e09\u4e2a\u6210\u679c\u90fd\u5c06\u968f .NET 8 \u4e00\u8d77\u53d1\u5e03\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4e3a Blazor Web \u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u4e86\u65b0\u7684Identity UI\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0e\u65b0\u7684\u6e32\u67d3\u6a21\u5f0f\uff08\u670d\u52a1\u5668\u548c WebAssembly \uff09\u914d\u5408\u4f7f\u7528\u3002 \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b .NET 8 \u4e2d\u7684\u65b0\u53d8\u5316\u6240\u652f\u6301\u7684\u4e00\u4e9b\u573a\u666f\u3002\u6211\u4eec\u5c06\u5728\u8fd9\u7bc7\u535a\u6587\u4e2d\u4ecb\u7ecd\uff1a \u786e\u4fdd\u7b80\u5355 Web [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/users\/99475"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/comments?post=534"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/posts\/534\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/media\/547"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/categories?post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet-ch\/wp-json\/wp\/v2\/tags?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}