ASP.NET Core Identity:
- 為支援使用者介面 (UI) 登入功能的 API。
- 管理使用者、密碼、設定檔資料、角色、宣告、權杖、電子郵件確認等等。
使用者可以使用儲存在 Identity 中的登入資訊或外部登入提供者來建立帳戶。 支援的外部登入提供者包括 Facebook、Google、Microsoft帳戶和 Twitter。
如需如何全域要求所有用戶進行驗證的資訊,請參閱 要求已驗證的使用者。
原始程式碼Identity可在 GitHub 上取得。 建立腳手架 Identity 並檢視產生的檔案,以檢閱範本與 Identity 的互動。
Identity 通常會使用 SQL Server 資料庫來設定,以儲存使用者名稱、密碼和設定檔資料。 或者,也可以使用另一個永續性存放區,例如:Azure 表格儲存體。
在本主題中,您將了解如何使用 Identity 來註冊、登入和登出使用者。 注意:範本會將使用者的使用者名稱和電子郵件視為是相同的。 如需建立使用 Identity之應用程式的詳細指示,請參閱 後續步驟。
如需了解在 Identity 應用程式中關於 Blazor 的更多資訊,請參閱 ASP.NET Core Blazor 驗證與授權 及 Blazor 文件中的後續文章。
ASP.NET Core Identity 與 Microsoft身分識別平台無關。 身分識別平臺Microsoft為:
- Azure Active Directory (Azure AD) 開發人員平台的演進。
- ASP.NET Core 應用程式中驗證和授權的替代身分識別解決方案。
ASP.NET Core Identity 會將使用者介面 (UI) 登入功能新增至 ASP.NET Core Web 應用程式。 若要保護 Web API 和 SPA,請使用下列其中一項:
- Microsoft Entra 身份識別
- Azure Active Directory B2C (Azure AD B2C)
- Duende Identity 伺服器
Duende Identity 伺服器是適用於 ASP.NET Core 的 OpenID Connect 和 OAuth 2.0 架構。 Duende Identity 伺服器會啟用下列安全性功能:
- 驗證即服務 (AaaS)
- 多個應用程式類型的單一登入/登出 (SSO)
- API 的存取控制
- 同盟閘道
重要
Duende Software 可能會要求您支付使用 Duende Identity 伺服器的授權費用。 如需詳細資訊,請參閱 從 .NET 5 中的 ASP.NET Core 移轉至 .NET 6。
如需詳細資訊,請參閱 Duende 伺服器檔(Duende Identity Software 網站)。
檢視或下載範例程式代碼 (如何下載)。
使用驗證建立Blazor Web App
使用個別帳戶建立 ASP.NET 核心 Blazor Web App 專案。
- 選擇Blazor Web App範本。 選取 下一步。
- 進行下列選擇:
- 驗證類型: 個別帳戶
- 互動式轉譯模式:伺服器
- 互動位置: 全域
- 選取 ,創建。
產生的專案包含 IdentityRazor 元件。 元件位於 Components/Account
資料夾中。 例如:
/Components/Account/Pages/Register
/Components/Account/Pages/Login
/Components/Account/Pages/Manage/ChangePassword
Identity
Razor 在特定使用案例的文件中會個別描述組件,並且每次版本更新時可能會改變。 當您使用個別帳戶產生 Blazor Web App 時,生成的專案會包含 IdentityRazor 元件。
Identity
Razor您也可以在 ASP.NET Core 參考來源 (BlazorGitHub 存放庫) 的專案dotnet/aspnetcore
範本中檢查元件。
備註
通常,指向 .NET 參考來源的文件連結會載入存放庫的預設分支,這代表 .NET 下一版本的最新開發進度。 若要選取特定發行版本的標籤,請使用「切換分支或標籤」下拉式清單。 如需詳細資訊,請參閱如何選取 ASP.NET Core 原始程式碼 (dotnet/AspNetCore.Docs #26205) 的版本標籤。
如需詳細資訊,請參閱 ASP.NET Core Blazor 的驗證和授權說明,以及後續的Blazor文件中的文章。 主要 ASP.NET Core 檔案集的安全性 和 Identity 區域中的大部分文章都適用於 Blazor 應用程式。 不過,Blazor 檔案集包含的文章和指引具有取代或補充資訊的功能。 建議您先研究一般 ASP.NET Core 文件集,然後讀取Blazor安全性和Identity文件中的文章。
使用身份驗證建立Razor Pages 應用程式
使用個別帳戶建立 ASP.NET 核心 Web 應用程式 (Razor Pages) 專案。
- 選取 「ASP.NET Core 網頁應用程式(Razor 頁面)」 範本。 選取 下一步。
- 針對 [驗證類型],選取 [個別帳戶]。
- 選取 ,創建。
產生的專案提供 ASP.NET Core Identity 作為 Razor 類別庫 (RCL) 。
Identity
Razor 類別庫會公開具有 Identity
區域的端點。 例如:
Areas/Identity/Pages/Account/Register
Areas/Identity/Pages/Account/Login
Areas/Identity/Pages/Account/Manage/ChangePassword
特定使用案例的文件中會個別描述頁面,而且每次發行版本時可能會有變更。 若要檢視 RCL 中的所有頁面,請參閱 ASP.NET Core 參考來源 (dotnet/aspnetcore
GitHub 存放庫, Identity/UI/src/Areas/Identity/Pages
資料夾) 。 您可以將單個頁面或所有頁面設定到應用程式中。 如需詳細資訊,請參閱 ASP.NET Core 專案中的 Scaffold Identity。
使用驗證建立MVC應用程式
使用個別帳戶建立 ASP.NET Core MVC 專案。
- 選取 ASP.NET Core Web 應用程式 (Model-View-Controller) 範本。 選取 下一步。
- 針對 [驗證類型],選取 [個別帳戶]。
- 選取 ,創建。
產生的專案提供 ASP.NET Core Identity 作為 Razor 類別庫 (RCL) 。 類別庫 IdentityRazor 是以頁面 Razor 為基礎,並通過區域 Identity
來公開端點。 例如:
Areas/Identity/Pages/Account/Register
Areas/Identity/Pages/Account/Login
Areas/Identity/Pages/Account/Manage/ChangePassword
特定使用案例的文件中會個別描述頁面,而且每次發行版本時可能會有變更。 若要檢視 RCL 中的所有頁面,請參閱 ASP.NET Core 參考來源 (dotnet/aspnetcore
GitHub 存放庫, Identity/UI/src/Areas/Identity/Pages
資料夾) 。 您可以將單個頁面或所有頁面設定到應用程式中。 如需詳細資訊,請參閱 ASP.NET Core 專案中的 Scaffold Identity。
套用遷移
套用移轉來初始化資料庫。
在套件管理員主控台 (PMC) 中執行下列命令:
Update-Database
測試註冊和登入
執行應用程式並註冊使用者。 視視窗大小而定,您可能需要選取瀏覽切換按鈕以查看 [註冊] 和 [登入] 連結。
檢視 Identity 資料庫
- 從 [檢視] 功能表中,選取 [SQL Server 物件總管] (SSOX)。
- 瀏覽至 [(localdb)MSSQLLocalDB(SQL Server 13)]。 以滑鼠右鍵按兩下 dbo。AspNetUsers>檢視數據:
設定 Identity 服務
服務已在 Program.cs
新增。 典型的模式是依下列順序呼叫方法:
Add{Service}
builder.Services.Configure{Service}
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using WebApp1.Data;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings.
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
builder.Services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
上述程式碼使用 Identity 預設選項值進行設定。 服務可透過 相依性插入提供給應用程式。
透過呼叫 Identity 啟用 UseAuthentication。
UseAuthentication
將驗證 中間件 新增至要求管線。
範本產生的應用程式不會使用 授權。 包含 app.UseAuthorization
是為了確保應用程式在新增授權時能以正確的順序新增。
UseRouting
、UseAuthentication
和 UseAuthorization
必須依上述程式碼所示的順序呼叫。
如需 的詳細資訊 IdentityOptions
,請參閱 IdentityOptions 和 應用程式啟動。
Scaffold註冊、登入、登出和註冊確認
檢查登記簿
當使用者按一下 頁面上的 [註冊]Register
按鈕時,即會叫用 RegisterModel.OnPostAsync
動作。 使用者是由 CreateAsync(TUser) 在 _userManager
物件上所建立:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
.ToList();
if (ModelState.IsValid)
{
var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code = code },
protocol: Request.Scheme);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
return RedirectToPage("RegisterConfirmation",
new { email = Input.Email });
}
else
{
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
停用預設帳戶驗證
使用預設範本時,系統會將使用者重新導向至 Account.RegisterConfirmation
他們可以選取連結以確認帳戶的位置。 默認值Account.RegisterConfirmation
僅用於測試,應該在生產應用程式中停用自動帳戶驗證。
若要在註冊時要求使用已確認的帳戶並防止立即登入,請在 DisplayConfirmAccountLink = false
中設定 /Areas/Identity/Pages/Account/RegisterConfirmation.cshtml.cs
:
[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
private readonly IEmailSender _sender;
public RegisterConfirmationModel(UserManager<IdentityUser> userManager, IEmailSender sender)
{
_userManager = userManager;
_sender = sender;
}
public string Email { get; set; }
public bool DisplayConfirmAccountLink { get; set; }
public string EmailConfirmationUrl { get; set; }
public async Task<IActionResult> OnGetAsync(string email, string returnUrl = null)
{
if (email == null)
{
return RedirectToPage("/Index");
}
var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
return NotFound($"Unable to load user with email '{email}'.");
}
Email = email;
// Once you add a real email sender, you should remove this code that lets you confirm the account
DisplayConfirmAccountLink = false;
if (DisplayConfirmAccountLink)
{
var userId = await _userManager.GetUserIdAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
EmailConfirmationUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
}
return Page();
}
}
登入
登入表單會在下列情況下顯示:
- 已選取 [登入] 連結。
- 用戶嘗試存取未獲授權存取的受限制頁面, 或 系統尚未驗證時。
提交 [登入] 頁面上的表單時,即會呼叫 OnPostAsync
動作。 在 PasswordSignInAsync
物件上呼叫 _signInManager
。
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout,
// set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.Email,
Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToPage("./LoginWith2fa", new
{
ReturnUrl = returnUrl,
RememberMe = Input.RememberMe
});
}
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToPage("./Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}
}
// If we got this far, something failed, redisplay form
return Page();
}
如需如何做出授權決策的資訊,請參閱 ASP.NET Core 中的授權簡介。
登出
註銷連結會叫用LogoutModel.OnPost
動作。
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace WebApp1.Areas.Identity.Pages.Account
{
[AllowAnonymous]
public class LogoutModel : PageModel
{
private readonly SignInManager<IdentityUser> _signInManager;
private readonly ILogger<LogoutModel> _logger;
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
{
_signInManager = signInManager;
_logger = logger;
}
public void OnGet()
{
}
public async Task<IActionResult> OnPost(string returnUrl = null)
{
await _signInManager.SignOutAsync();
_logger.LogInformation("User logged out.");
if (returnUrl != null)
{
return LocalRedirect(returnUrl);
}
else
{
return RedirectToPage();
}
}
}
}
在上述程式代碼中,程式代碼 return RedirectToPage();
必須是重新導向,讓瀏覽器執行新的要求,並更新使用者的身分識別。
SignOutAsync 清除儲存在 cookie 中的使用者宣告。
在 Pages/Shared/_LoginPartial.cshtml
中指定了帖子:
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index"
title="Manage">Hello @User.Identity.Name!</a>
</li>
<li class="nav-item">
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout"
asp-route-returnUrl="@Url.Page("/", new { area = "" })"
method="post" >
<button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
</li>
}
</ul>
測試 Identity
預設 Web 專案範本允許匿名存取首頁。 若要測試 Identity,請新增 [Authorize]
:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace WebApp1.Pages
{
[Authorize]
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
如果您已登入,請登出。執行應用程式並選取 Privacy 連結。 您會被重新導向至登入頁面。
探索 Identity
若要更詳細地探索 Identity:
- 建立完整的身分識別UI來源
- 檢查每個頁面的來源,並逐步執行偵錯工具。
Identity 元件
Identity所有相依的 NuGet 套件都包含在 ASP.NET Core 共用架構中。
的主要套件Identity是 Microsoft.AspNetCore.Identity。 此套件包含 ASP.NET Core Identity 的核心介面集,且由 Microsoft.AspNetCore.Identity.EntityFrameworkCore
所包含。
移轉至 ASP.NET Core Identity
如需移轉現有 Identity 存放區的詳細資訊和指引,請參閱移轉 驗證和 Identity。
設定密碼強度
請參閱設定以查看設定最低密碼要求的範例。
AddDefaultIdentity 和 AddIdentity
AddDefaultIdentity 是在 ASP.NET Core 2.1 中引進。 呼叫 AddDefaultIdentity
類似於呼叫下列項目:
如需詳細資訊,請參閱 AddDefaultIdentity 來源 。
防止靜態 Identity 資產發佈
若要防止將靜態 Identity 資產 (Identity UI 的樣式表單和 JavaScript 檔案) 發佈至 Web 根目錄,請將下列 ResolveStaticWebAssetsInputsDependsOn
屬性和 RemoveIdentityAssets
目標新增至應用程式的專案檔:
<PropertyGroup>
<ResolveStaticWebAssetsInputsDependsOn>RemoveIdentityAssets</ResolveStaticWebAssetsInputsDependsOn>
</PropertyGroup>
<Target Name="RemoveIdentityAssets">
<ItemGroup>
<StaticWebAsset Remove="@(StaticWebAsset)" Condition="%(SourceId) == 'Microsoft.AspNetCore.Identity.UI'" />
</ItemGroup>
</Target>
後續步驟
- 如需使用 SQLite 進行設定Identity的資訊,請參閱如何Identity設定 SQLite (
dotnet/AspNetCore.Docs
#5131)。 - 配置 Identity
- 使用受授權保護的用戶數據建立 ASP.NET Core 應用程式
- 在 ASP.NET Core 專案中新增、下載和刪除用戶數據Identity
- 在 ASP.NET Core 中啟用 TOTP 驗證器應用程式的 QR 代碼產生
- 將驗證和 Identity 移轉至 ASP.NET Core
- ASP.NET Core 中的帳戶確認和密碼復原
- ASP.NET Core 中的多重要素驗證
- 在 Web 伺服器陣列中裝載 ASP.NET Core
由 里克·安德森
ASP.NET Core Identity:
- 為支援使用者介面 (UI) 登入功能的 API。
- 管理使用者、密碼、設定檔資料、角色、宣告、權杖、電子郵件確認等等。
使用者可以使用儲存在 Identity 中的登入資訊或外部登入提供者來建立帳戶。 支援的外部登入提供者包括 Facebook、Google、Microsoft帳戶和 Twitter。
如需如何全域要求所有用戶進行驗證的資訊,請參閱 要求已驗證的使用者。
原始程式碼Identity可在 GitHub 上取得。 建立腳手架 Identity 並檢視產生的檔案,以檢閱範本與 Identity 的互動。
Identity 通常會使用 SQL Server 資料庫來設定,以儲存使用者名稱、密碼和設定檔資料。 或者,也可以使用另一個永續性存放區,例如:Azure 表格儲存體。
在本主題中,您將了解如何使用 Identity 來註冊、登入和登出使用者。 注意:範本會將使用者的使用者名稱和電子郵件視為是相同的。 如需建立使用 Identity之應用程式的詳細指示,請參閱 後續步驟。
ASP.NET Core Identity 與 Microsoft身分識別平台無關。 身分識別平臺Microsoft為:
- Azure Active Directory (Azure AD) 開發人員平台的演進。
- ASP.NET Core 應用程式中驗證和授權的替代身分識別解決方案。
ASP.NET Core Identity 會將使用者介面 (UI) 登入功能新增至 ASP.NET Core Web 應用程式。 若要保護 Web API 和 SPA,請使用下列其中一項:
- Microsoft Entra 身份識別
- Azure Active Directory B2C (Azure AD B2C)
- Duende Identity 伺服器
Duende Identity 伺服器是適用於 ASP.NET Core 的 OpenID Connect 和 OAuth 2.0 架構。 Duende Identity 伺服器會啟用下列安全性功能:
- 驗證即服務 (AaaS)
- 多個應用程式類型的單一登入/登出 (SSO)
- API 的存取控制
- 同盟閘道
重要
Duende Software 可能會要求您支付使用 Duende Identity 伺服器的授權費用。 如需詳細資訊,請參閱 從 .NET 5 中的 ASP.NET Core 移轉至 .NET 6。
如需詳細資訊,請參閱 Duende 伺服器檔(Duende Identity Software 網站)。
檢視或下載範例程式代碼 (如何下載)。
建立具有驗證功能的 Web 應用程式
使用個別使用者帳戶建立 ASP.NET Core Web 應用程式。
- 選取 [ASP.NET Core Web 應用程式] 範本。 將專案命名為 WebApp1 ,其命名空間與專案下載相同。 按一下 [確定]。
- 在 [驗證類型] 輸入中,選取 [個別使用者帳戶]。
產生的專案提供 ASP.NET Core Identity 作為 Razor 類別庫。
Identity
Razor 類別庫會公開具有 Identity
區域的端點。 例如:
- /Identity/帳戶/登入
- /Identity/Account/Logout
- /Identity/帳戶/管理
套用遷移
套用移轉來初始化資料庫。
在套件管理員主控台 (PMC) 中執行下列命令:
Update-Database
測試註冊和登入
執行應用程式並註冊使用者。 視視窗大小而定,您可能需要選取瀏覽切換按鈕以查看 [註冊] 和 [登入] 連結。
檢視 Identity 資料庫
- 從 [檢視] 功能表中,選取 [SQL Server 物件總管] (SSOX)。
- 瀏覽至 [(localdb)MSSQLLocalDB(SQL Server 13)]。 以滑鼠右鍵按兩下 dbo。AspNetUsers>檢視數據:
設定 Identity 服務
服務已在 Program.cs
新增。 典型的模式是依下列順序呼叫方法:
Add{Service}
builder.Services.Configure{Service}
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using WebApp1.Data;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
builder.Services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings.
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
builder.Services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
上述程式碼使用 Identity 預設選項值進行設定。 服務可透過 相依性插入提供給應用程式。
透過呼叫 Identity 啟用 UseAuthentication。
UseAuthentication
將驗證 中間件 新增至要求管線。
範本產生的應用程式不會使用 授權。 包含 app.UseAuthorization
是為了確保應用程式在新增授權時能以正確的順序新增。
UseRouting
、UseAuthentication
和 UseAuthorization
必須依上述程式碼所示的順序呼叫。
如需 的詳細資訊 IdentityOptions
,請參閱 IdentityOptions 和 應用程式啟動。
Scaffold註冊、登入、登出和註冊確認
檢查登記簿
當使用者按一下 頁面上的 [註冊]Register
按鈕時,即會叫用 RegisterModel.OnPostAsync
動作。 使用者是由 CreateAsync(TUser) 在 _userManager
物件上所建立:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
.ToList();
if (ModelState.IsValid)
{
var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code = code },
protocol: Request.Scheme);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
return RedirectToPage("RegisterConfirmation",
new { email = Input.Email });
}
else
{
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
停用預設帳戶驗證
使用預設範本時,系統會將使用者重新導向至 Account.RegisterConfirmation
他們可以選取連結以確認帳戶的位置。 默認值Account.RegisterConfirmation
僅用於測試,應該在生產應用程式中停用自動帳戶驗證。
若要在註冊時要求使用已確認的帳戶並防止立即登入,請在 DisplayConfirmAccountLink = false
中設定 /Areas/Identity/Pages/Account/RegisterConfirmation.cshtml.cs
:
[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
private readonly IEmailSender _sender;
public RegisterConfirmationModel(UserManager<IdentityUser> userManager, IEmailSender sender)
{
_userManager = userManager;
_sender = sender;
}
public string Email { get; set; }
public bool DisplayConfirmAccountLink { get; set; }
public string EmailConfirmationUrl { get; set; }
public async Task<IActionResult> OnGetAsync(string email, string returnUrl = null)
{
if (email == null)
{
return RedirectToPage("/Index");
}
var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
return NotFound($"Unable to load user with email '{email}'.");
}
Email = email;
// Once you add a real email sender, you should remove this code that lets you confirm the account
DisplayConfirmAccountLink = false;
if (DisplayConfirmAccountLink)
{
var userId = await _userManager.GetUserIdAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
EmailConfirmationUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
}
return Page();
}
}
登入
登入表單會在下列情況下顯示:
- 已選取 [登入] 連結。
- 用戶嘗試存取未獲授權存取的受限制頁面, 或 系統尚未驗證時。
提交 [登入] 頁面上的表單時,即會呼叫 OnPostAsync
動作。 在 PasswordSignInAsync
物件上呼叫 _signInManager
。
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout,
// set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.Email,
Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToPage("./LoginWith2fa", new
{
ReturnUrl = returnUrl,
RememberMe = Input.RememberMe
});
}
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToPage("./Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}
}
// If we got this far, something failed, redisplay form
return Page();
}
如需如何做出授權決策的資訊,請參閱 ASP.NET Core 中的授權簡介。
登出
註銷連結會叫用LogoutModel.OnPost
動作。
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace WebApp1.Areas.Identity.Pages.Account
{
[AllowAnonymous]
public class LogoutModel : PageModel
{
private readonly SignInManager<IdentityUser> _signInManager;
private readonly ILogger<LogoutModel> _logger;
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
{
_signInManager = signInManager;
_logger = logger;
}
public void OnGet()
{
}
public async Task<IActionResult> OnPost(string returnUrl = null)
{
await _signInManager.SignOutAsync();
_logger.LogInformation("User logged out.");
if (returnUrl != null)
{
return LocalRedirect(returnUrl);
}
else
{
return RedirectToPage();
}
}
}
}
在上述程式代碼中,程式代碼 return RedirectToPage();
必須是重新導向,讓瀏覽器執行新的要求,並更新使用者的身分識別。
SignOutAsync 清除儲存在 cookie 中的使用者宣告。
在 Pages/Shared/_LoginPartial.cshtml
中指定了帖子:
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index"
title="Manage">Hello @User.Identity.Name!</a>
</li>
<li class="nav-item">
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout"
asp-route-returnUrl="@Url.Page("/", new { area = "" })"
method="post" >
<button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
</li>
}
</ul>
測試 Identity
預設 Web 專案範本允許匿名存取首頁。 若要測試 Identity,請新增 [Authorize]
:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace WebApp1.Pages
{
[Authorize]
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
如果您已登入,請登出。執行應用程式並選取 Privacy 連結。 您會被重新導向至登入頁面。
探索 Identity
若要更詳細地探索 Identity:
- 建立完整的身分識別UI來源
- 檢查每個頁面的來源,並逐步執行偵錯工具。
Identity 元件
Identity所有相依的 NuGet 套件都包含在 ASP.NET Core 共用架構中。
的主要套件Identity是 Microsoft.AspNetCore.Identity。 此套件包含 ASP.NET Core Identity 的核心介面集,且由 Microsoft.AspNetCore.Identity.EntityFrameworkCore
所包含。
移轉至 ASP.NET Core Identity
如需移轉現有 Identity 存放區的詳細資訊和指引,請參閱移轉 驗證和 Identity。
設定密碼強度
請參閱設定以查看設定最低密碼要求的範例。
AddDefaultIdentity 和 AddIdentity
AddDefaultIdentity 是在 ASP.NET Core 2.1 中引進。 呼叫 AddDefaultIdentity
類似於呼叫下列項目:
如需詳細資訊,請參閱 AddDefaultIdentity 來源 。
防止靜態 Identity 資產發佈
若要防止將靜態 Identity 資產 (Identity UI 的樣式表單和 JavaScript 檔案) 發佈至 Web 根目錄,請將下列 ResolveStaticWebAssetsInputsDependsOn
屬性和 RemoveIdentityAssets
目標新增至應用程式的專案檔:
<PropertyGroup>
<ResolveStaticWebAssetsInputsDependsOn>RemoveIdentityAssets</ResolveStaticWebAssetsInputsDependsOn>
</PropertyGroup>
<Target Name="RemoveIdentityAssets">
<ItemGroup>
<StaticWebAsset Remove="@(StaticWebAsset)" Condition="%(SourceId) == 'Microsoft.AspNetCore.Identity.UI'" />
</ItemGroup>
</Target>
後續步驟
由 里克·安德森
ASP.NET Core Identity:
- 為支援使用者介面 (UI) 登入功能的 API。
- 管理使用者、密碼、設定檔資料、角色、宣告、權杖、電子郵件確認等等。
使用者可以使用儲存在 Identity 中的登入資訊或外部登入提供者來建立帳戶。 支援的外部登入提供者包括 Facebook、Google、Microsoft帳戶和 Twitter。
如需如何全域要求所有用戶進行驗證的資訊,請參閱 要求已驗證的使用者。
原始程式碼Identity可在 GitHub 上取得。 建立腳手架 Identity 並檢視產生的檔案,以檢閱範本與 Identity 的互動。
Identity 通常會使用 SQL Server 資料庫來設定,以儲存使用者名稱、密碼和設定檔資料。 或者,也可以使用另一個永續性存放區,例如:Azure 表格儲存體。
在本主題中,您將了解如何使用 Identity 來註冊、登入和登出使用者。 注意:範本會將使用者的使用者名稱和電子郵件視為是相同的。 如需建立使用 Identity之應用程式的詳細指示,請參閱 後續步驟。
- Azure Active Directory (Azure AD) 開發人員平台的演進。
- ASP.NET Core 應用程式中驗證和授權的替代身分識別解決方案。
- 與 ASP.NET Core Identity 無關。
ASP.NET Core Identity 會將使用者介面 (UI) 登入功能新增至 ASP.NET Core Web 應用程式。 若要保護 Web API 和 SPA,請使用下列其中一項:
- Microsoft Entra 身份識別
- Azure Active Directory B2C (Azure AD B2C)
- Duende IdentityServer。 Duende IdentityServer 是協力廠商產品。
Duende IdentityServer 是適用於 ASP.NET Core 的 OpenID Connect 和 OAuth 2.0 架構。 Duende IdentityServer 會啟用下列安全性功能:
- 驗證即服務 (AaaS)
- 多個應用程式類型的單一登入/登出 (SSO)
- API 的存取控制
- 同盟閘道
如需詳細資訊,請參閱 Duende IdentityServer 概觀。
如需其他驗證提供者的詳細資訊,請參閱 ASP.NET Core 的社群 OSS 驗證選項
檢視或下載範例程式代碼 (如何下載)。
建立具有驗證功能的 Web 應用程式
使用個別使用者帳戶建立 ASP.NET Core Web 應用程式。
- 選取 [檔案]> [新增]> [專案]。
- 選取 [ASP.NET Core Web 應用程式]。 將專案命名為 WebApp1 ,其命名空間與專案下載相同。 按一下 [確定]。
- 選取 ASP.NET Core [Web 應用程式],然後選取 [變更驗證]。
- 選取 [個別使用者帳戶],然後按一下 [確定]。
產生的專案提供 ASP.NET Core Identity 作為 Razor 類別庫。
Identity
Razor 類別庫會公開具有 Identity
區域的端點。 例如:
- /Identity/帳戶/登入
- /Identity/Account/Logout
- /Identity/帳戶/管理
套用遷移
套用移轉來初始化資料庫。
在套件管理員主控台 (PMC) 中執行下列命令:
PM> Update-Database
測試註冊和登入
執行應用程式並註冊使用者。 視視窗大小而定,您可能需要選取瀏覽切換按鈕以查看 [註冊] 和 [登入] 連結。
檢視 Identity 資料庫
- 從 [檢視] 功能表中,選取 [SQL Server 物件總管] (SSOX)。
- 瀏覽至 [(localdb)MSSQLLocalDB(SQL Server 13)]。 以滑鼠右鍵按兩下 dbo。AspNetUsers>檢視數據:
設定 Identity 服務
服務已在 ConfigureServices
新增。 典型模式是呼叫所有 Add{Service}
方法,然後呼叫 services.Configure{Service}
方法。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
// options.UseSqlite(
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings.
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
}
上述醒目提示的程式碼使用 Identity 預設選項值進行設定。 服務可透過 相依性插入提供給應用程式。
透過呼叫 Identity 啟用 UseAuthentication。
UseAuthentication
將驗證 中間件 新增至要求管線。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
// options.UseSqlite(
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddRazorPages();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings.
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
}
上述程式碼使用 Identity 預設選項值進行設定。 服務可透過 相依性插入提供給應用程式。
透過呼叫 Identity 啟用 UseAuthentication。
UseAuthentication
將驗證 中間件 新增至要求管線。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
範本產生的應用程式不會使用 授權。 包含 app.UseAuthorization
是為了確保應用程式在新增授權時能以正確的順序新增。
UseRouting
、UseAuthentication
、UseAuthorization
和 UseEndpoints
必須依上述程式碼所示的順序呼叫。
如需有關IdentityOptions
和Startup
的詳細資訊,請參閱IdentityOptions和應用程式啟動。
Scaffold註冊、登入、登出和註冊確認
檢查登記簿
當使用者按一下 頁面上的 [註冊]Register
按鈕時,即會叫用 RegisterModel.OnPostAsync
動作。 使用者是由 CreateAsync(TUser) 在 _userManager
物件上所建立:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync())
.ToList();
if (ModelState.IsValid)
{
var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code = code },
protocol: Request.Scheme);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
return RedirectToPage("RegisterConfirmation",
new { email = Input.Email });
}
else
{
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
停用預設帳戶驗證
使用預設範本時,系統會將使用者重新導向至 Account.RegisterConfirmation
他們可以選取連結以確認帳戶的位置。 默認值Account.RegisterConfirmation
僅用於測試,應該在生產應用程式中停用自動帳戶驗證。
若要在註冊時要求使用已確認的帳戶並防止立即登入,請在 DisplayConfirmAccountLink = false
中設定 /Areas/Identity/Pages/Account/RegisterConfirmation.cshtml.cs
:
[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
private readonly IEmailSender _sender;
public RegisterConfirmationModel(UserManager<IdentityUser> userManager, IEmailSender sender)
{
_userManager = userManager;
_sender = sender;
}
public string Email { get; set; }
public bool DisplayConfirmAccountLink { get; set; }
public string EmailConfirmationUrl { get; set; }
public async Task<IActionResult> OnGetAsync(string email, string returnUrl = null)
{
if (email == null)
{
return RedirectToPage("/Index");
}
var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
return NotFound($"Unable to load user with email '{email}'.");
}
Email = email;
// Once you add a real email sender, you should remove this code that lets you confirm the account
DisplayConfirmAccountLink = false;
if (DisplayConfirmAccountLink)
{
var userId = await _userManager.GetUserIdAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
EmailConfirmationUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
}
return Page();
}
}
登入
登入表單會在下列情況下顯示:
- 已選取 [登入] 連結。
- 用戶嘗試存取未獲授權存取的受限制頁面, 或 系統尚未驗證時。
提交 [登入] 頁面上的表單時,即會呼叫 OnPostAsync
動作。 在 PasswordSignInAsync
物件上呼叫 _signInManager
。
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout,
// set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(Input.Email,
Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToPage("./LoginWith2fa", new
{
ReturnUrl = returnUrl,
RememberMe = Input.RememberMe
});
}
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToPage("./Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return Page();
}
}
// If we got this far, something failed, redisplay form
return Page();
}
如需如何做出授權決策的資訊,請參閱 ASP.NET Core 中的授權簡介。
登出
註銷連結會叫用LogoutModel.OnPost
動作。
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace WebApp1.Areas.Identity.Pages.Account
{
[AllowAnonymous]
public class LogoutModel : PageModel
{
private readonly SignInManager<IdentityUser> _signInManager;
private readonly ILogger<LogoutModel> _logger;
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
{
_signInManager = signInManager;
_logger = logger;
}
public void OnGet()
{
}
public async Task<IActionResult> OnPost(string returnUrl = null)
{
await _signInManager.SignOutAsync();
_logger.LogInformation("User logged out.");
if (returnUrl != null)
{
return LocalRedirect(returnUrl);
}
else
{
return RedirectToPage();
}
}
}
}
在上述程式代碼中,程式代碼 return RedirectToPage();
必須是重新導向,讓瀏覽器執行新的要求,並更新使用者的身分識別。
SignOutAsync 清除儲存在 cookie 中的使用者宣告。
在 Pages/Shared/_LoginPartial.cshtml
中指定了帖子:
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index"
title="Manage">Hello @User.Identity.Name!</a>
</li>
<li class="nav-item">
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout"
asp-route-returnUrl="@Url.Page("/", new { area = "" })"
method="post" >
<button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
</li>
}
</ul>
測試 Identity
預設 Web 專案範本允許匿名存取首頁。 若要測試 Identity,請新增 [Authorize]
:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
namespace WebApp1.Pages
{
[Authorize]
public class PrivacyModel : PageModel
{
private readonly ILogger<PrivacyModel> _logger;
public PrivacyModel(ILogger<PrivacyModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
如果您已登入,請登出。執行應用程式並選取 Privacy 連結。 您會被重新導向至登入頁面。
探索 Identity
若要更詳細地探索 Identity:
- 建立完整的身分識別UI來源
- 檢查每個頁面的來源,並逐步執行偵錯工具。
Identity 元件
Identity所有相依的 NuGet 套件都包含在 ASP.NET Core 共用架構中。
的主要套件Identity是 Microsoft.AspNetCore.Identity。 此套件包含 ASP.NET Core Identity 的核心介面集,且由 Microsoft.AspNetCore.Identity.EntityFrameworkCore
所包含。
移轉至 ASP.NET Core Identity
如需移轉現有 Identity 存放區的詳細資訊和指引,請參閱移轉 驗證和 Identity。
設定密碼強度
請參閱設定以查看設定最低密碼要求的範例。
防止靜態 Identity 資產發佈
若要防止將靜態 Identity 資產 (Identity UI 的樣式表單和 JavaScript 檔案) 發佈至 Web 根目錄,請將下列 ResolveStaticWebAssetsInputsDependsOn
屬性和 RemoveIdentityAssets
目標新增至應用程式的專案檔:
<PropertyGroup>
<ResolveStaticWebAssetsInputsDependsOn>RemoveIdentityAssets</ResolveStaticWebAssetsInputsDependsOn>
</PropertyGroup>
<Target Name="RemoveIdentityAssets">
<ItemGroup>
<StaticWebAsset Remove="@(StaticWebAsset)" Condition="%(SourceId) == 'Microsoft.AspNetCore.Identity.UI'" />
</ItemGroup>
</Target>
後續步驟
- ASP.NET Core Identity 原始程式碼
- AddDefaultIdentity 原始碼
- 如需有關配置 以使用 SQLite 的資訊,請參閱Identity。
- 配置 Identity
- 使用受授權保護的用戶數據建立 ASP.NET Core 應用程式
- 在 ASP.NET Core 專案中新增、下載和刪除用戶數據Identity
- 在 ASP.NET Core 中啟用 TOTP 驗證器應用程式的 QR 代碼產生
- 將驗證和 Identity 移轉至 ASP.NET Core
- ASP.NET Core 中的帳戶確認和密碼復原
- 在 ASP.NET Core 中使用 SMS 進行雙因素驗證
- 在 Web 伺服器陣列中裝載 ASP.NET Core