在不使用 ASP.NET Core Identity 的情况下使用社交登录提供程序身份验证
作者:Kirk Larkin 和 Rick Anderson
ASP.NET Core 中的 Facebook 和 Google 身份验证说明了如何使用户通过结合使用 OAuth 2.0 与来自外部身份验证提供程序的凭据进行登录。 该文章中所述的方法包含了作为身份验证提供程序的 ASP.NET Core Identity。
此示例演示如何在不使用 ASP.NET Core Identity 的情况下使用外部身份验证提供程序。 此方法适用于不需要所有 ASP.NET Core Identity 功能,但仍需要与受信任的外部身份验证提供程序集成的应用。
此示例使用 Google 身份验证来对用户进行身份验证。 使用 Google 身份验证可将管理登录过程的许多复杂性转移到 Google。 若要与不同的外部身份验证提供程序集成,请参阅以下文章:
配置
在 Program.cs
中,使用 AddAuthentication、AddCookie 和 AddGoogle 方法配置应用的身份验证方案:
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];
});
builder.Services.AddRazorPages();
对 AddAuthentication 的调用将设置应用的 DefaultScheme。 DefaultScheme
是以下 HttpContext
身份验证扩展方法使用的默认方案:
如果将应用的 DefaultScheme
设置为 CookieAuthenticationDefaults.AuthenticationScheme(“Cookie”),则会将该应用配置为将 Cookie 用作对这些扩展方法的默认方案。 将应用的 DefaultChallengeScheme 设置为 GoogleDefaults.AuthenticationScheme(“Google”)会将应用配置为将 Google 用作对 ChallengeAsync
的调用的默认方案。 DefaultChallengeScheme
会替代 DefaultScheme
。 有关设置时替代 DefaultScheme
的更多属性,请参阅 AuthenticationOptions。
在 Program.cs
中,调用 UseAuthentication 和 UseAuthorization。 此中间件组合设置 HttpContext.User 并针对请求运行授权中间件:
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
若要详细了解身份验证方案,请参阅身份验证概念。 若要了解有关 cookie 身份验证的详细信息,请参阅在不使用 ASP.NET Core Identity 的情况下使用 cookie 身份验证。
应用授权
通过向控制器、操作或页面应用 [Authorize] 特性来测试应用的身份验证配置。 以下代码限制仅允许经过身份验证的用户访问 Privacy 页面:
[Authorize]
public class PrivacyModel : PageModel
{
}
保存访问令牌
SaveTokens 定义在授权成功后,是否应在 AuthenticationProperties 中存储访问令牌和刷新令牌。 SaveTokens
默认设置为 false
,以减少最终身份验证 cookie 的大小。
若要在成功授权后保存访问令牌和刷新令牌,请在 Program.cs
中将 SaveTokens
设置为 true
:
builder.Services
.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];
options.SaveTokens = true;
});
若要检索已保存的令牌,请使用 GetTokenAsync。 以下示例检索名为 access_token
的令牌:
public async Task OnGetAsync()
{
var accessToken = await HttpContext.GetTokenAsync(
GoogleDefaults.AuthenticationScheme, "access_token");
// ...
}
注销
若要注销当前用户并删除他们的 cookie,请调用 SignOutAsync。 以下代码将 Logout
页面处理程序添加到索引页面:
public class IndexModel : PageModel
{
public async Task<IActionResult> OnPostLogoutAsync()
{
// using Microsoft.AspNetCore.Authentication;
await HttpContext.SignOutAsync();
return RedirectToPage();
}
}
请注意,对 SignOutAsync
的调用不会指定身份验证方案。 应用使用 DefaultScheme
CookieAuthenticationDefaults.AuthenticationScheme
作为回退。
其他资源
ASP.NET Core 中的 Facebook 和 Google 身份验证说明了如何使用户通过结合使用 OAuth 2.0 与来自外部身份验证提供程序的凭据进行登录。 该文章中所述的方法包含了作为身份验证提供程序的 ASP.NET Core Identity。
此示例演示如何在不使用 ASP.NET Core Identity 的情况下使用外部身份验证提供程序。 此方法适用于不需要所有 ASP.NET Core Identity 功能,但仍需要与受信任的外部身份验证提供程序集成的应用。
此示例使用 Google 身份验证来对用户进行身份验证。 使用 Google 身份验证可将管理登录过程的许多复杂性转移到 Google。 若要与不同的外部身份验证提供程序集成,请参阅以下文章:
配置
在 ConfigureServices
方法中,使用 AddAuthentication、AddCookie 和 AddGoogle 方法配置应用的身份验证方案:
public void ConfigureServices(IServiceCollection services)
{
// requires
// using Microsoft.AspNetCore.Authentication.Cookies;
// using Microsoft.AspNetCore.Authentication.Google;
// NuGet package Microsoft.AspNetCore.Authentication.Google
services
.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(options =>
{
options.ClientId = Configuration["Authentication:Google:ClientId"];
options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
services.AddRazorPages();
}
对 AddAuthentication 的调用将设置应用的 DefaultScheme。 DefaultScheme
是以下 HttpContext
身份验证扩展方法使用的默认方案:
如果将应用的 DefaultScheme
设置为 CookieAuthenticationDefaults.AuthenticationScheme(“Cookie”),则会将该应用配置为将 Cookie 用作对这些扩展方法的默认方案。 将应用的 DefaultChallengeScheme 设置为 GoogleDefaults.AuthenticationScheme(“Google”)会将应用配置为将 Google 用作对 ChallengeAsync
的调用的默认方案。 DefaultChallengeScheme
会替代 DefaultScheme
。 有关设置时替代 DefaultScheme
的更多属性,请参阅 AuthenticationOptions。
在 Startup.Configure
中,在调用 UseRouting 和 UseEndpoints 之间调用 UseAuthentication 和 UseAuthorization。 此中间件组合设置 HttpContext.User 并针对请求运行授权中间件:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
若要详细了解身份验证方案,请参阅身份验证概念。 若要了解有关 cookie 身份验证的详细信息,请参阅在不使用 ASP.NET Core Identity 的情况下使用 cookie 身份验证。
应用授权
通过向控制器、操作或页面应用 [Authorize] 特性来测试应用的身份验证配置。 以下代码限制仅允许经过身份验证的用户访问 Privacy 页面:
[Authorize]
public class PrivacyModel : PageModel
{
}
注销
若要注销当前用户并删除他们的 cookie,请调用 SignOutAsync。 以下代码将 Logout
页面处理程序添加到索引页面:
public class IndexModel : PageModel
{
public async Task<IActionResult> OnPostLogoutAsync()
{
await HttpContext.SignOutAsync();
return RedirectToPage();
}
}
请注意,对 SignOutAsync
的调用不会指定身份验证方案。 应用的 CookieAuthenticationDefaults.AuthenticationScheme
的 DefaultScheme
用作回退。
其他资源
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈