在不 ASP.NET Core 标识的情况下使用社交登录提供程序身份验证

ASP.NET Core 中的 Facebook、Google 和外部提供程序身份验证 介绍如何使用户能够使用 OAuth 2.0 通过外部身份验证提供程序中的凭据进行登录。 该主题中所述的方法包括 ASP.NET Core 标识作为身份验证提供程序。

此示例演示如何使用外部身份验证提供程序,而无需ASP.NET Core 标识。 这对于不需要 ASP.NET Core 标识的所有功能,但仍需要与受信任的外部身份验证提供程序集成的应用很有用。

此示例使用Google 身份验证对用户进行身份验证。 使用 Google 身份验证将管理登录过程的许多复杂性转移到 Google。 若要与其他外部身份验证提供程序集成,请参阅以下主题:

配置

ConfigureServices 方法中,使用 AddAuthenticationAddCookieAddGoogle 方法配置应用的身份验证方案:

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的默认方案。 DefaultScheme``DefaultChallengeScheme 重写。 有关在设置时覆盖 DefaultScheme 的其他属性,请参阅 AuthenticationOptions

Startup.Configure中,调用 UseRoutingUseEndpoints之间的 UseAuthenticationUseAuthorization 这会设置 HttpContext.User 属性,并为请求运行授权中间件:

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
});

若要详细了解身份验证方案,请参阅身份验证概念 若要详细了解 cookie 身份验证,请参阅 使用 cookie 而无需 ASP.NET Core 标识的身份验证

应用授权

通过将 AuthorizeAttribute 特性应用到控制器、操作或页来测试应用的身份验证配置。 以下代码将访问隐私页面的权限限制为已经过身份验证的用户:

[Authorize]
public class PrivacyModel : PageModel
{
    
}

注销

若要注销当前用户并删除其 cookie,请调用SignOutAsync 下面的代码将 Logout 页处理程序添加到索引页:

public class IndexModel : PageModel
{
    public async Task<IActionResult> OnPostLogoutAsync()
    {
        await HttpContext.SignOutAsync();
        return RedirectToPage();
    }
}

请注意,对 SignOutAsync 的调用未指定身份验证方案。 应用程序的 DefaultScheme CookieAuthenticationDefaults.AuthenticationScheme 用作回退。

其他资源

ASP.NET Core 中的 Facebook、Google 和外部提供程序身份验证 介绍如何使用户能够使用 OAuth 2.0 通过外部身份验证提供程序中的凭据进行登录。 该主题中所述的方法包括 ASP.NET Core 标识作为身份验证提供程序。

此示例演示如何使用外部身份验证提供程序,而无需ASP.NET Core 标识。 这对于不需要 ASP.NET Core 标识的所有功能,但仍需要与受信任的外部身份验证提供程序集成的应用很有用。

此示例使用Google 身份验证对用户进行身份验证。 使用 Google 身份验证将管理登录过程的许多复杂性转移到 Google。 若要与其他外部身份验证提供程序集成,请参阅以下主题:

配置

ConfigureServices 方法中,使用 AddAuthenticationAddCookieAddGoogle 方法配置应用的身份验证方案:

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"];
    });

AddAuthentication的调用将设置应用的DefaultScheme DefaultScheme 是以下 HttpContext 身份验证扩展方法使用的默认方案:

如果将应用程序的 DefaultScheme 设置为CookieAuthenticationDefaults. AuthenticationScheme ("cookie"),则会将应用程序配置为使用 cookie 作为这些扩展方法的默认方案。 如果将应用程序的 DefaultChallengeScheme 设置为GoogleDefaults. AuthenticationScheme ("Google"),则会将应用程序配置为使用 Google 作为调用 ChallengeAsync的默认方案。 DefaultScheme``DefaultChallengeScheme 重写。 有关在设置时覆盖 DefaultScheme 的其他属性,请参阅 AuthenticationOptions

Configure 方法中,调用 UseAuthentication 方法来调用设置 HttpContext.User 属性的身份验证中间件。 在调用 UseMvcWithDefaultRouteUseMvc 之前调用 UseAuthentication 方法:

app.UseAuthentication();

若要详细了解身份验证方案,请参阅身份验证概念 若要详细了解 cookie 身份验证,请参阅 使用 cookie 而无需 ASP.NET Core 标识的身份验证

应用授权

通过将 AuthorizeAttribute 特性应用到控制器、操作或页来测试应用的身份验证配置。 以下代码将访问隐私页面的权限限制为已经过身份验证的用户:

[Authorize]
public class PrivacyModel : PageModel
{
    
}

注销

若要注销当前用户并删除其 cookie,请调用SignOutAsync 下面的代码将 Logout 页处理程序添加到索引页:

public class IndexModel : PageModel
{
    public async Task<IActionResult> OnPostLogoutAsync()
    {
        await HttpContext.SignOutAsync();
        return RedirectToPage();
    }
}

请注意,对 SignOutAsync 的调用未指定身份验证方案。 应用程序的 DefaultScheme CookieAuthenticationDefaults.AuthenticationScheme 用作回退。

其他资源

上一篇:使用 cookie 而无需 ASP.NET Core 标识的身份验证

下一篇:Azure 应用服务中的身份验证和授权

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程