ASP.NET Core React 模板 v3.15:没有 IdentityServer 也安全?

Ben*_*iFB 6 asp.net-identity .net-core asp.net-core identityserver4 asp.net-core-identity

最近的某个时候,Microsoft 开始在 ASP.NET Core React 模板(在 Identity Core 之上)中包含 IdentityServer 功能。我试图清楚地理解这种差异的含义。因此,我从附在此处的模板开始:

https://drive.google.com/file/d/1GV4UNhsPp_PdPk2RKjoz0490Vn9A6geY/view?usp=sharing

并做了一些事情:

- 搭建所有身份资产

-注释掉startup.cs中的以下几行:

(在配置服务中)

        //services.AddIdentityServer()
        //    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

        services.AddAuthentication();
            //.AddIdentityServerJwt();
Run Code Online (Sandbox Code Playgroud)

(在配置中)

        //app.UseIdentityServer();
Run Code Online (Sandbox Code Playgroud)

在客户端,我删除了一些代码,并在 FetchData.js 中进行了此更改:

  async populateWeatherData() {
    //const token = await authService.getAccessToken();
    const response = await fetch('weatherforecast', {
        //headers: !token ? {} : { 'Authorization': `Bearer ${token}` },
        credentials: 'same-origin',
    });
    const data = await response.json();
    this.setState({ forecasts: data, loading: false });
  }
}
Run Code Online (Sandbox Code Playgroud)

我的理解是,登录后,我们将拥有一个包含登录信息的 cookie,并使用凭证:'same-origin' 导致将其发送到受保护的 API 端点。

这对我来说有效并且似乎很安全。去测试:

- 启动项目 - 导航到 /identity/account/login (这不会自动发生,因为我删除了其他客户端代码) - 为自己创建一个帐户。(出现提示时,您必须应用数据库迁移。)然后使用该帐户登录。

现在,您可以单击“获取数据”,数据将会加载。为了确认这实际上是安全的,上面说

    credentials: 'same-origin',
Run Code Online (Sandbox Code Playgroud)

将值替换为“省略”。然后重复该过程。手动导航到登录页面并单击“获取数据”。它会失败。

基于此,我想确认在 ASP.NET Core React 项目模板中,我能够在不利用 IdentityServer 的情况下实现相同的 API 端点安全性。它是否正确?

如果是这样,利用 IdentityServer 可以增加什么价值?我的假设是,此模板中的一切都运行良好,因为我们通过 Web 表单进行身份验证并将令牌存储在 cookie 中,但 IdentityServer 为我们提供了其他场景的灵活性(我们在此模板中并未真正利用)。

让事情变得更复杂一些,在这篇文章中比较 ASP.NET Identity Core 和 Identity Server,

.NET Core 身份与 IdentityServer4

答案是:“ASP.NET Core Identity 是一个会员系统,它不提供任何现成的端点,也不提供令牌管理或对不同授权方式的支持。”

但根据我在这里展示的内容,ASP.NET Core Identity 似乎确实提供了一种授权机制,并且我相信令牌管理,因为它返回给我一个令牌,我将其存储在 cookie 中并在以下情况下传回:我想到达一个终点。

再次总结我的问题:

- 在没有 Identity Server 的情况下,我修改后的示例是否与使用 Identity Server 时一样安全?

- 如果是,在此模板的上下文中利用 Identity Server 的附加价值是什么?

-如果是的话,两者之间存在一些重叠是否准确?

谢谢...

Sau*_*edy 1

我在他们的文档中看到了这一部分,其中详细描述了 ASP.NET Core 中可用的身份验证选项之间的差异。

https://learn.microsoft.com/en-us/aspnet/core/security/how-to-choose-identity-solution?view=aspnetcore-7.0

读完它后我感觉好多了,并证实了我认为我们都有的感觉。我们不需要 IdentityServer4 来正确保护我们的 API。

我遇到了同样的问题,和你一样,我觉得我不知道使用 IdentityServer4 来实现简单的 SPA 有什么附加价值。

更新:

Microsoft 将在 .NET 8 中删除对 IdentityServer 的依赖。

https://devblogs.microsoft.com/dotnet/improvements-auth-identity-aspnetcore-8/