auth0-aspnetcore-authentication
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAuth0 ASP.NET Core Web App Integration
Auth0 ASP.NET Core Web应用集成
Add login, logout, and user profile to an ASP.NET Core MVC, Razor Pages, or Blazor Server application using .
Auth0.AspNetCore.Authentication使用为ASP.NET Core MVC、Razor Pages或Blazor Server应用添加登录、登出和用户资料功能。
Auth0.AspNetCore.AuthenticationPrerequisites
前置条件
- ASP.NET Core application (.NET 8 or higher)
- Auth0 Regular Web Application configured (not an API - must be an Application)
- If you don't have Auth0 set up yet, use the skill first
auth0-quickstart
- ASP.NET Core应用(.NET 8或更高版本)
- 已配置Auth0常规Web应用(非API,必须是应用类型)
- 若尚未设置Auth0,请先使用技能
auth0-quickstart
When NOT to Use
不适用场景
- ASP.NET Core Web APIs with JWT Bearer validation - Use for JWT-protected REST APIs
auth0-aspnetcore-api - Blazor WebAssembly - Requires OIDC client-side auth; see the Auth0 Blazor WebAssembly quickstart
- Single Page Applications - Use ,
auth0-react, orauth0-vuefor client-side authauth0-angular - Next.js applications - Use which handles both client and server
auth0-nextjs - Python web apps - Use for Flask or see the Django quickstart
auth0-flask
- 带JWT Bearer验证的ASP.NET Core Web API - 针对受JWT保护的REST API,请使用
auth0-aspnetcore-api - Blazor WebAssembly - 需要OIDC客户端认证;请查看Auth0 Blazor WebAssembly快速入门文档
- 单页应用(SPA) - 客户端认证请使用、
auth0-react或auth0-vueauth0-angular - Next.js应用 - 使用,它同时支持客户端和服务端认证
auth0-nextjs - Python Web应用 - Flask应用请使用,Django应用请查看对应快速入门文档
auth0-flask
Quick Start Workflow
快速入门流程
1. Install SDK
1. 安装SDK
bash
dotnet add package Auth0.AspNetCore.Authenticationbash
dotnet add package Auth0.AspNetCore.Authentication2. Configure Credentials
2. 配置凭证
Add Auth0 settings to :
appsettings.jsonjson
{
"Auth0": {
"Domain": "your-tenant.us.auth0.com",
"ClientId": "your_client_id",
"ClientSecret": "your_client_secret"
}
}For local development, keep secrets out of source control - use to avoid committing :
dotnet user-secretsClientSecretbash
dotnet user-secrets set "Auth0:Domain" "your-tenant.us.auth0.com"
dotnet user-secrets set "Auth0:ClientId" "your_client_id"
dotnet user-secrets set "Auth0:ClientSecret" "your_client_secret"Auth0:Domainhttps://Auth0:ClientIdAuth0:ClientSecret将Auth0设置添加到:
appsettings.jsonjson
{
"Auth0": {
"Domain": "your-tenant.us.auth0.com",
"ClientId": "your_client_id",
"ClientSecret": "your_client_secret"
}
}本地开发时,请将密钥放在源代码控制之外 - 使用避免提交:
dotnet user-secretsClientSecretbash
dotnet user-secrets set "Auth0:Domain" "your-tenant.us.auth0.com"
dotnet user-secrets set "Auth0:ClientId" "your_client_id"
dotnet user-secrets set "Auth0:ClientSecret" "your_client_secret"Auth0:Domainhttps://Auth0:ClientIdAuth0:ClientSecret3. Configure Auth0 Dashboard
3. 配置Auth0控制台
In your Auth0 Application settings:
- Allowed Callback URLs:
http://localhost:5000/callback - Allowed Logout URLs:
http://localhost:5000 - Allowed Web Origins:
http://localhost:5000
在你的Auth0应用设置中:
- 允许回调URL:
http://localhost:5000/callback - 允许登出URL:
http://localhost:5000 - 允许Web源:
http://localhost:5000
4. Register Auth0 in Program.cs
4. 在Program.cs中注册Auth0
csharp
using Auth0.AspNetCore.Authentication;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuth0WebAppAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"];
options.ClientId = builder.Configuration["Auth0:ClientId"];
options.ClientSecret = builder.Configuration["Auth0:ClientSecret"];
});
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Standard middleware...
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication(); // Must come before UseAuthorization
app.UseAuthorization(); // Critical: order matters
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();Critical: must come before . Reversing these causes silent auth failures where protected routes are never challenged.
UseAuthentication()UseAuthorization()csharp
using Auth0.AspNetCore.Authentication;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuth0WebAppAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"];
options.ClientId = builder.Configuration["Auth0:ClientId"];
options.ClientSecret = builder.Configuration["Auth0:ClientSecret"];
});
builder.Services.AddControllersWithViews();
var app = builder.Build();
// 标准中间件...
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication(); // 必须在UseAuthorization之前调用
app.UseAuthorization(); // 重要:顺序不能颠倒
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();重要提示:必须在之前调用。颠倒顺序会导致静默认证失败,受保护的路由永远不会触发认证挑战。
UseAuthentication()UseAuthorization()5. Create AccountController
5. 创建AccountController
csharp
using Auth0.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
public class AccountController : Controller
{
public async Task Login(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
}
[Authorize]
public async Task Logout()
{
var authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
[Authorize]
public IActionResult Profile()
{
return View();
}
}Login[Authorize]Logout[Authorize]SignOutAsynccsharp
using Auth0.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
public class AccountController : Controller
{
public async Task Login(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
}
[Authorize]
public async Task Logout()
{
var authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
[Authorize]
public IActionResult Profile()
{
return View();
}
}Login[Authorize]Logout[Authorize]SignOutAsync6. Create Profile View
6. 创建Profile视图
Create :
Views/Account/Profile.cshtmlhtml
@{
ViewData["Title"] = "User Profile";
}
<div class="row">
<div class="col-md-2">
<img src="@User.FindFirst(c => c.Type == "picture")?.Value"
alt="Profile picture" class="img-fluid rounded-circle" />
</div>
<div class="col-md-10">
<h3>@User.Identity.Name</h3>
<p><strong>Email:</strong>
@User.FindFirst(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value</p>
<p><strong>User ID:</strong>
@User.FindFirst(c => c.Type == System.Security.Claims.ClaimTypes.NameIdentifier)?.Value</p>
</div>
</div>
<h4 class="mt-4">Claims</h4>
<table class="table">
<thead><tr><th>Claim Type</th><th>Claim Value</th></tr></thead>
<tbody>
@foreach (var claim in User.Claims)
{
<tr><td>@claim.Type</td><td>@claim.Value</td></tr>
}
</tbody>
</table>创建:
Views/Account/Profile.cshtmlhtml
@{
ViewData["Title"] = "User Profile";
}
<div class="row">
<div class="col-md-2">
<img src="@User.FindFirst(c => c.Type == "picture")?.Value"
alt="Profile picture" class="img-fluid rounded-circle" />
</div>
<div class="col-md-10">
<h3>@User.Identity.Name</h3>
<p><strong>Email:</strong>
@User.FindFirst(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value</p>
<p><strong>User ID:</strong>
@User.FindFirst(c => c.Type == System.Security.Claims.ClaimTypes.NameIdentifier)?.Value</p>
</div>
</div>
<h4 class="mt-4">Claims</h4>
<table class="table">
<thead><tr><th>Claim Type</th><th>Claim Value</th></tr></thead>
<tbody>
@foreach (var claim in User.Claims)
{
<tr><td>@claim.Type</td><td>@claim.Value</td></tr>
}
</tbody>
</table>7. Update Navigation (_Layout.cshtml)
7. 更新导航栏(_Layout.cshtml)
Add login/logout/profile links to your nav bar inside :
_Layout.cshtmlhtml
@if (User.Identity.IsAuthenticated)
{
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Account" asp-action="Profile">@User.Identity.Name</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Account" asp-action="Logout">Logout</a>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Account" asp-action="Login">Login</a>
</li>
}在的导航栏中添加登录/登出/资料链接:
_Layout.cshtmlhtml
@if (User.Identity.IsAuthenticated)
{
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Account" asp-action="Profile">@User.Identity.Name</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Account" asp-action="Logout">Logout</a>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Account" asp-action="Login">Login</a>
</li>
}8. Test the App
8. 测试应用
bash
dotnet runVisit and click Login to start the Auth0 login flow.
http://localhost:5000bash
dotnet run访问并点击Login启动Auth0登录流程。
http://localhost:5000Blazor Server Variant
Blazor Server变体
For Blazor Server apps, use Razor Pages as auth endpoints - Blazor components cannot perform the HTTP redirects required by OAuth challenges.
对于Blazor Server应用,请使用Razor Pages作为认证端点——Blazor组件无法执行OAuth挑战所需的HTTP重定向。
Additional Program.cs Setup
额外的Program.cs设置
csharp
using Auth0.AspNetCore.Authentication;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuth0WebAppAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"];
options.ClientId = builder.Configuration["Auth0:ClientId"];
options.ClientSecret = builder.Configuration["Auth0:ClientSecret"];
});
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddCascadingAuthenticationState(); // Required for Blazor auth state
builder.Services.AddRazorPages(); // Required for auth endpoints
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();csharp
using Auth0.AspNetCore.Authentication;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuth0WebAppAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"];
options.ClientId = builder.Configuration["Auth0:ClientId"];
options.ClientSecret = builder.Configuration["Auth0:ClientSecret"];
});
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddCascadingAuthenticationState(); // Blazor认证状态必需
builder.Services.AddRazorPages(); // 认证端点必需
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();Login Razor Page (Pages/Login.cshtml.cs)
登录Razor页面(Pages/Login.cshtml.cs)
csharp
using Auth0.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class LoginModel : PageModel
{
public async Task OnGet(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
}
}csharp
using Auth0.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class LoginModel : PageModel
{
public async Task OnGet(string returnUrl = "/")
{
var authenticationProperties = new LoginAuthenticationPropertiesBuilder()
.WithRedirectUri(returnUrl)
.Build();
await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
}
}Logout Razor Page (Pages/Logout.cshtml.cs)
登出Razor页面(Pages/Logout.cshtml.cs)
csharp
using Auth0.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class LogoutModel : PageModel
{
public async Task OnGet()
{
var authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Content("~/"))
.Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
}csharp
using Auth0.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
public class LogoutModel : PageModel
{
public async Task OnGet()
{
var authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
.WithRedirectUri(Url.Content("~/"))
.Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
}Profile Component (Components/Pages/Profile.razor)
资料组件(Components/Pages/Profile.razor)
razor
@page "/profile"
@attribute [Authorize]
@using System.Security.Claims
<h1>Profile</h1>
<AuthorizeView>
<Authorized>
<div class="row">
<div class="col-2">
<img src="@context.User.FindFirst("picture")?.Value"
alt="Profile" class="img-fluid rounded-circle" />
</div>
<div class="col-10">
<h3>@context.User.Identity?.Name</h3>
<p><strong>Email:</strong> @context.User.FindFirst(ClaimTypes.Email)?.Value</p>
</div>
</div>
<h4 class="mt-4">Claims</h4>
<table class="table">
<thead><tr><th>Type</th><th>Value</th></tr></thead>
<tbody>
@foreach (var claim in context.User.Claims)
{
<tr><td>@claim.Type</td><td>@claim.Value</td></tr>
}
</tbody>
</table>
</Authorized>
</AuthorizeView>razor
@page "/profile"
@attribute [Authorize]
@using System.Security.Claims
<h1>Profile</h1>
<AuthorizeView>
<Authorized>
<div class="row">
<div class="col-2">
<img src="@context.User.FindFirst("picture")?.Value"
alt="Profile" class="img-fluid rounded-circle" />
</div>
<div class="col-10">
<h3>@context.User.Identity?.Name</h3>
<p><strong>Email:</strong> @context.User.FindFirst(ClaimTypes.Email)?.Value</p>
</div>
</div>
<h4 class="mt-4">Claims</h4>
<table class="table">
<thead><tr><th>Type</th><th>Value</th></tr></thead>
<tbody>
@foreach (var claim in context.User.Claims)
{
<tr><td>@claim.Type</td><td>@claim.Value</td></tr>
}
</tbody>
</table>
</Authorized>
</AuthorizeView>Update MainLayout.razor Navigation
更新MainLayout.razor导航
razor
@using Microsoft.AspNetCore.Components.Authorization
<AuthorizeView>
<Authorized>
<a href="/profile">@context.User.Identity?.Name</a>
<a href="/Logout">Logout</a>
</Authorized>
<NotAuthorized>
<a href="/Login">Login</a>
</NotAuthorized>
</AuthorizeView>razor
@using Microsoft.AspNetCore.Components.Authorization
<AuthorizeView>
<Authorized>
<a href="/profile">@context.User.Identity?.Name</a>
<a href="/Logout">Logout</a>
</Authorized>
<NotAuthorized>
<a href="/Login">Login</a>
</NotAuthorized>
</AuthorizeView>Routes.razor
Routes.razor
Wrap the in to enable authorization throughout the component tree:
RouterCascadingAuthenticationStaterazor
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
</CascadingAuthenticationState>将包裹在中,以便在整个组件树中启用授权:
RouterCascadingAuthenticationStaterazor
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>
</CascadingAuthenticationState>Razor Pages Variant
Razor Pages变体
For Razor Pages apps (without Blazor), use instead of in . Auth endpoints are the same Login/Logout page models shown in the Blazor Server section. Replace navigation in using the same check shown in the MVC section.
AddRazorPages()AddControllersWithViews()Program.cs_Layout.cshtmlUser.Identity.IsAuthenticated对于Razor Pages应用(不含Blazor),在中使用替代。认证端点与Blazor Server部分所示的Login/Logout页面模型相同。使用MVC部分所示的检查更新中的导航。
Program.csAddRazorPages()AddControllersWithViews()User.Identity.IsAuthenticated_Layout.cshtmlCommon Mistakes
常见错误
| Mistake | Fix |
|---|---|
Hardcoding | Read from configuration - use |
Committing | Use |
| Must call |
| Signing out of only one scheme | Always call both |
Adding | |
| Not configuring Callback URLs in Auth0 Dashboard | Must add |
Passing | |
Not adding | Required for Blazor Server - without it, |
| Using Blazor components for login/logout redirects | Blazor components cannot perform HTTP redirects - use Razor Pages ( |
Not adding | Login and Logout Razor Pages won't be routed without these registrations |
Using | That package is for JWT-protected APIs - use |
Using | |
Not creating | MVC requires the directory to exist before creating the view |
| 错误 | 修复方案 |
|---|---|
在源代码中硬编码 | 从配置中读取——使用 |
将 | 对客户端密钥使用 |
| 必须先调用 |
| 仅登出一个方案 | 务必同时调用 |
为 | |
| 未在Auth0控制台中配置回调URL | 必须将 |
传递带 | |
Blazor中未添加 | Blazor Server必需——没有它, |
| 使用Blazor组件处理登录/登出重定向 | Blazor组件无法执行HTTP重定向——使用Razor Pages( |
Blazor中未添加 | 没有这些注册,Login和Logout Razor Pages将无法被路由 |
对Web应用使用 | 该包用于受JWT保护的API——基于会话的Web应用请使用 |
使用 | |
未为Profile视图创建 | MVC要求在创建视图前目录已存在 |
Key SDK Methods
关键SDK方法
| Method/Property | Usage | Purpose |
|---|---|---|
| | Registers Auth0 cookie-based authentication |
| | Builds properties for the login challenge |
| | Builds properties for the logout redirect |
| | Initiates the Auth0 Universal Login redirect |
| | Signs out of Auth0 and redirects to logout URL |
| | Clears the local session cookie |
| | Accesses individual user claims in controllers/views |
| | Checks authentication state in views/layouts |
| | Protects routes requiring authentication |
| | Required for Blazor Server auth state propagation |
| 方法/属性 | 使用方式 | 用途 |
|---|---|---|
| | 注册基于Cookie的Auth0认证 |
| | 构建登录挑战的属性 |
| | 构建登出重定向的属性 |
| | 启动Auth0通用登录重定向 |
| | 登出Auth0并重定向到登出URL |
| | 清除本地会话Cookie |
| | 在控制器/视图中访问单个用户声明 |
| | 在视图/布局中检查认证状态 |
| 控制器动作或Razor组件上的 | 保护需要认证的路由 |
| | Blazor Server认证状态传播必需 |
Related Skills
相关技能
- - For ASP.NET Core Web APIs with JWT Bearer token validation
auth0-aspnetcore-api - - For server-rendered Express web apps with login/logout sessions
auth0-express - - For Flask web applications with session-based auth
auth0-flask
- - 用于带JWT Bearer令牌验证的ASP.NET Core Web API
auth0-aspnetcore-api - - 用于带登录/登出会话的服务器渲染Express Web应用
auth0-express - - 用于基于会话认证的Flask Web应用
auth0-flask
Quick Reference
快速参考
SDK registration:
csharp
builder.Services.AddAuth0WebAppAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"]; // required
options.ClientId = builder.Configuration["Auth0:ClientId"]; // required
options.ClientSecret = builder.Configuration["Auth0:ClientSecret"]; // required
});Login action:
csharp
var props = new LoginAuthenticationPropertiesBuilder().WithRedirectUri(returnUrl).Build();
await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, props);Logout action (always call both):
csharp
var props = new LogoutAuthenticationPropertiesBuilder().WithRedirectUri(Url.Action("Index", "Home")).Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, props);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);Route protection:
csharp
[Authorize]
public IActionResult Profile() { return View(); }appsettings.json configuration keys:
- - Auth0 tenant domain (e.g.,
Auth0:Domain)tenant.us.auth0.com - - Application client ID
Auth0:ClientId - - Application client secret (use user-secrets in development)
Auth0:ClientSecret
SDK注册:
csharp
builder.Services.AddAuth0WebAppAuthentication(options =>
{
options.Domain = builder.Configuration["Auth0:Domain"]; // 必填
options.ClientId = builder.Configuration["Auth0:ClientId"]; // 必填
options.ClientSecret = builder.Configuration["Auth0:ClientSecret"]; // 必填
});登录动作:
csharp
var props = new LoginAuthenticationPropertiesBuilder().WithRedirectUri(returnUrl).Build();
await HttpContext.ChallengeAsync(Auth0Constants.AuthenticationScheme, props);登出动作(务必同时调用两个方法):
csharp
var props = new LogoutAuthenticationPropertiesBuilder().WithRedirectUri(Url.Action("Index", "Home")).Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, props);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);路由保护:
csharp
[Authorize]
public IActionResult Profile() { return View(); }appsettings.json配置项:
- - Auth0租户域名(例如
Auth0:Domain)tenant.us.auth0.com - - 应用客户端ID
Auth0:ClientId - - 应用客户端密钥(开发环境使用用户密钥)
Auth0:ClientSecret
Detailed Documentation
详细文档
- Setup Guide - Automated setup scripts, credential configuration, Auth0 CLI usage
- Integration Guide - Protected routes, calling APIs, Blazor patterns, error handling
- API Reference - Complete SDK configuration, builder options, claims reference
- 设置指南 - 自动化设置脚本、凭证配置、Auth0 CLI使用方法
- 集成指南 - 受保护路由、API调用、Blazor模式、错误处理
- API参考 - 完整SDK配置、构建器选项、声明参考