gpt4 book ai didi

angular - 如何将用户身份验证添加到 .NET Core 2.1 Angular 应用程序?

转载 作者:行者123 更新时间:2023-12-01 23:36:31 24 4
gpt4 key购买 nike

我使用的是 Visual Studio 2017,因为在 Angular 模板中禁用了 Individual User Account 选项 我在想如何向它添加用户身份验证?我刚开始使用 VS 2017 和 .Net Core 2.1,所以我在网上搜索是否有关于如何操作的文档,但我找不到任何东西。

我看到一个答案,我只需要在命令提示符中输入 dotnet new angular --auth Individual 但我无法使用它,因为我使用的是 Window 8.1 和命令适用于 .Net Core 3 和 VS 2019

我已经阅读了所有关于这个主题的谷歌搜索结果,但没有任何帮助。我什至使用其他搜索引擎希望得到更好的结果,但一无所获。

最佳答案

将 Dotnet 核心 3.0 与 Entityframework 核心 3.0 和 Identityserver4 结合使用,您可以拥有一个以 Angular 作为前端、Asp.net Core 3.0 作为后端的应用程序。查看此链接

Authentication and authorization for SPAs

首先你需要从here下载dotnet core 3.0 sdk .

然后打开命令提示符并使用

dotnet new angular -o <output_directory_name> -au Individual

这将为您创建项目。

现在在 Visual Studio 中转到“工具”>“选项”>“环境”>“预览功能”并检查

Use Previews of the .NET Core SDK (Requires Restart)

enter image description here

并重新启动 visual studio。

之后打开项目目录中的 .csProj 文件并点击保存。这将要求您保存在 visual studio 中打开项目时刚刚创建的解决方案文件。

将 .sln 文件保存在与 .csproj 相同的目录中,然后清理解决方案并重建解决方案。

您将拥有以 Asp.net Core 作为后端的 Angular 应用程序,并为您提供开箱即用的身份验证和授权。

这是您完成所有步骤后应用程序的示例屏幕截图

enter image description here

请注意,在项目目录中,您将拥有一个 app.db 文件,并且在 startup.cs 的 ConfigureServices 方法中,您将看到

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

模板默认使用 Sqlite,您可以通过安装 Microsoft.EntityFrameworkCore.SqlServer NuGet 包并在 startup.cs 文件中更改它来将其更改为 Sql server

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

并确保更新 appsettings.json 文件中的连接字符串。

关于angular - 如何将用户身份验证添加到 .NET Core 2.1 Angular 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com