C# MVC客户端和Api在IdentityServer4中协同工作

C# MVC客户端和Api在IdentityServer4中协同工作,c#,identityserver4,C#,Identityserver4,我正在使用IdentityServer4创建Identity server,我有一个使用Identity server安全性的MVC项目,但我也在同一个MVC项目中公开了一个API,我已经阅读并遵循了切换到混合流并重新添加API访问的指示,但我没有得到它。 我需要的是让一个MVC客户端和一个Api在同一个项目上协同工作,两者都受到identity项目安全性的保护。这是可能的吗?是的,我们可以使用您可以观察到的安全措施。还有其他方法 //文件夹位置 创建文件夹以存放模型、控制器、数据和迁移。还建

我正在使用IdentityServer4创建Identity server,我有一个使用Identity server安全性的MVC项目,但我也在同一个MVC项目中公开了一个API,我已经阅读并遵循了切换到混合流并重新添加API访问的指示,但我没有得到它。
我需要的是让一个MVC客户端和一个Api在同一个项目上协同工作,两者都受到identity项目安全性的保护。这是可能的吗?

是的,我们可以使用您可以观察到的安全措施。还有其他方法

//文件夹位置
创建文件夹以存放模型、控制器、数据和迁移。还建议您为名为Areas的API创建一个文件夹
现在我们已经设置了文件夹,让我们从创建UserStore开始。我们需要它来验证我们的用户并生成令牌。最好的方法之一是创建一个从IdentityUser扩展的用户类。
//为identity server用户添加模型
使用Microsoft.AspNetCore.Identity;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
命名空间TokenAuthASPCore.Models
{
公共类应用程序用户:IdentityUser
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
}
另外,让我们创建一个Todo类,以便稍后测试RESTAPI。
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
命名空间TokenAuthASPCore.Models
{
公共课待办事项
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共bool已完成{get;set;}
}
}
建立我们的数据库
为了能够同时使用这两种功能,我们必须首先连接到数据库。我将使用MS SQL Server。
向appsettings.json文件添加一个连接字符串,该字符串指定要与之交互的数据库的位置。
“连接字符串”:{
“TestConnection”:“数据源=DESKTOP-GIIMG7O\\JAMES;初始目录=IdentityBlog;集成安全性=True”
}
然后转到Startup.cs文件并添加数据库连接的配置。
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“TestConnection”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
IdentityServer 4配置
下一步是配置IdentityServer4。IdentityServer4是一个框架,允许我们向APS.NET核心应用程序添加OIDC身份验证和授权。它允许生成JWT令牌,并支持许多Oauth 2流。我们将使用ResourceOwnerPassword流,因为在后面的教程中,我们将使用React和Angular应用程序以及Xamarin应用程序进行身份验证。
我们首先创建一个Config.cs文件。这将保存IdentityServer 4的大部分配置。
使用IdentityServer4;
使用IdentityServer4.Models;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
命名空间令牌AuthAspCore
{
公共类配置
{
公共静态IEnumerable GetIdentityResources()
{
返回新列表
{
新标识资源.OpenId(),
新标识资源.Email(),
新标识资源.Profile(),
};
}
公共静态IEnumerable GetApiResources()
{
返回新列表
{
新的API资源(“api1”、“我的API”)
};
}
公共静态IEnumerable GetClients()
{
//客户端凭据客户端
返回新列表
{
//资源所有者密码授予客户端
新客户
{
ClientId=“ro.angular”,
AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,
客户秘密=
{
新密码(“Secret.Sha256())
},
允许范围={
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.Address,
“api1”
}
}
};
}
}
}
IdentityResources表示我们可以从标识存储中获得哪些信息,我们的API资源表示我们可以访问的API信息,客户端表示允许哪些客户端使用我们的API进行身份验证。
如果我们跳回Startup.cs文件,现在就可以将IdentityServer4注册为服务。这是在ConfigureServices方法中配置的。
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryStedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.addAsNetIdentity();
services.AddMvc();
services.AddAuthentication(选项=>
{
options.DefaultAuthenticateScheme=JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(选项=>
{
//您的identityserver的基本地址
选项。权限=”http://localhost:52718/";
//API资源的名称
期权。澳元