我已经搜遍了如何UserService在asp.net核心中注册一个IdentityServer4,但我似乎无法找到正确的方法来做到这一点.
这是注册发现InMemoryUsers代码在这里,但是我想从样品中定义我的MSSQL数据库不是静态的用户访问的用户.
var builder = services.AddIdentityServer(options =>
{
options.SigningCertificate = cert;
});
builder.AddInMemoryClients(Clients.Get());
builder.AddInMemoryScopes(Scopes.Get());
builder.AddInMemoryUsers(Users.Get());
Run Code Online (Sandbox Code Playgroud)
那么我看了一下这是针对IdentityServer3的.
var factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get());
var userService = new UserService();
factory.UserService = new Registration<IUserService>(resolver => userService);
Run Code Online (Sandbox Code Playgroud)
从在线阅读看起来我需要使用DI系统注册UserService,但我不确定它如何绑定到IdentityServer,例如.
services.AddScoped<IUserService, UserService>();
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
如何将我绑定UserService到构建器(IdentityServer4用户)?我将如何调用我的数据库来访问和验证我现有的数据库用户UserService(我使用存储库连接到数据库)?
考虑到这一点必须与asp.net核心一起使用.
谢谢!
我正在使用IdentityServer4.
我想添加其他自定义声明来访问令牌,但我无法做到这一点.我修改了Quickstart5,并按照下面的Coemgen的建议,通过ProfileService添加了ASP.NET Identity Core和自定义声明.
你可以在这里下载我的代码:[zip package] [3].(它基于:带有ASP.NET Identity Core的Quickstart5以及通过ProfileService添加的声明).
问题:GetProfileDataAsync未执行.
我正在尝试使用授权代码流实现带有AspNet Core的Identity Server 4.
问题是,github上的IdentityServer4存储库有几个样本,但没有一个带有授权代码流.
有没有人有关于如何使用Identity Server 4和MVC中的客户端实现授权代码流的示例?
我正在尝试重定向到 IdentityServer 进行授权,并在重定向 URL 中获取“需要代码质询”。
错误消息显示invalid_request需要代码质询,还有我的重定向 URL http://localhost:44367/signin-oidc#error=invalid_request&error_description=code%20challenge%20required&state=CfDJ8Cq6lLUEMhZLqMhFVN
这是我的客户端配置:
namespace TestClient
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = …Run Code Online (Sandbox Code Playgroud) oauth openid-connect asp.net-core identityserver4 asp.net-core-3.0
我正在研究使用Identity Server 4在基于C#的MVC应用程序中进行身份验证.我想使用存储在Azure AD中的帐户作为有效用户的来源,但文档似乎只是指Google和OpenID,并且仅提及Azure.
有没有人知道在与Identity Server 4一起使用Azure AD的过程中如何使用Azure AD的任何好的文档和/或教程?
概括
尝试发布具有身份验证功能的基本 .NET Core React 应用程序时,我收到 IdentityServer 错误。这是使用 dotnet new react --auth Individual,.Net Core 3.0 Preview5,并按照此处和此处的说明进行操作。
我已经通过 TLS/SSL 设置上传了 PFX。我还确保证书适用于我的开发环境,发现GetMyX509Certificate返回证书。
运行 app.UseIdentityServer() 时,导致问题的代码似乎在 Startup.cs 中的配置中:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
Log.Information("Configure - running app.Use: Authentication, IdentityServer");
Log.Information("Configure - running app.Use: Authentication");
app.UseAuthentication();
Log.Information("Configure - running app.Use: IdentityServer");
app.UseIdentityServer();
//CODE DOESN'T MAKE IT HERE!!!
Log.Information("Configure - running app.UseMvc");
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
Log.Information("Configure - running app.UseSpa");
app.UseSpa(spa …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用IdentityServer4实现"基于角色的授权",以基于用户角色访问我的API.
例如,我想为用户提供两个角色,即FreeUser和PaidUser,并希望通过授权属性使用[Authorize(Roles ="FreeUser"))]来访问API,请帮助我如何实现这一点.
我有以下解决方案结构:
我已经注册了我的Javascript客户端,如下所示:
new Client
{
ClientId = "js",
ClientName = "javascript client",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser= true,
RedirectUris = {"http://localhost:5004/callback.html"},
PostLogoutRedirectUris = {"http://localhost:5004/index.html"},
AllowedCorsOrigins = {"http://localhost:5004"},
AllowedScopes =
{
StandardScopes.OpenId.Name,
StandardScopes.Profile.Name,
"api1",
"role",
StandardScopes.AllClaims.Name
}
}
Run Code Online (Sandbox Code Playgroud)
领域
return new List<Scope>
{
StandardScopes.OpenId,
StandardScopes.Profile,
new Scope
{
Name = "api1",
Description = "My API"
},
new Scope
{
Enabled = true,
Name = "role",
DisplayName = "Role(s)",
Description = "roles of user",
Type = ScopeType.Identity, …Run Code Online (Sandbox Code Playgroud) 我希望能够从docker机器外部和内部对Identity Server(STS)进行身份验证.
我在设置容器内外的正确权限时遇到问题.如果我将权限设置为内部名称,mcoidentityserver:5000则API可以进行身份验证,但客户端无法获取令牌,因为客户端位于docker网络之外.如果我将权限设置为外部名称,localhost:5000则客户端可以获取令牌,但API无法识别权限名称(因为localhost在这种情况下是主机).
我该如何设置权限?或许我需要调整码头网络?
我正在设置一个Windows 10 docker开发环境,它使用ASP.NET Core API(在Linux上),Identity Server 4(Linux上的ASP.NET Core)和PostgreSQL数据库.PostgreSQL不是问题,包含在图表中以保证完整性.它被映射到9876,因为我现在还在主机上运行了一个PostgreSQL实例.mco是我们公司的简称.
我一直在遵循Identity Server 4的说明来启动和运行.
我不包括docker-compose.debug.yml它,因为它运行的命令只与在Visual Studio中运行相关.
泊坞窗,compose.yml
version: '2'
services:
mcodatabase:
image: mcodatabase
build:
context: ./Data
dockerfile: Dockerfile
restart: always
ports:
- 9876:5432
environment:
POSTGRES_USER: mcodevuser
POSTGRES_PASSWORD: password
POSTGRES_DB: mcodev
volumes:
- postgresdata:/var/lib/postgresql/data
networks:
- mconetwork
mcoidentityserver:
image: mcoidentityserver
build:
context: ./Mco.IdentityServer
dockerfile: Dockerfile
ports:
- 5000:5000
networks:
- mconetwork
mcoapi: …Run Code Online (Sandbox Code Playgroud) 我有一个使用IdentityServer4进行令牌验证的API.我想用内存中的TestServer对这个API进行单元测试.我想将IdentityServer托管在内存中的TestServer中.
我已经设法从IdentityServer创建一个令牌.
这是我到底有多远,但是我收到错误"无法从http:// localhost:54100/.well-known/openid-configuration获取配置 "
Api使用[授权] - 属性与不同的策略.这是我想测试的.
可以这样做,我做错了什么?我试图查看IdentityServer4的源代码,但没有遇到过类似的集成测试场景.
protected IntegrationTestBase()
{
var startupAssembly = typeof(Startup).GetTypeInfo().Assembly;
_contentRoot = SolutionPathUtility.GetProjectPath(@"<my project path>", startupAssembly);
Configure(_contentRoot);
var orderApiServerBuilder = new WebHostBuilder()
.UseContentRoot(_contentRoot)
.ConfigureServices(InitializeServices)
.UseStartup<Startup>();
orderApiServerBuilder.Configure(ConfigureApp);
OrderApiTestServer = new TestServer(orderApiServerBuilder);
HttpClient = OrderApiTestServer.CreateClient();
}
private void InitializeServices(IServiceCollection services)
{
var cert = new X509Certificate2(Path.Combine(_contentRoot, "idsvr3test.pfx"), "idsrv3test");
services.AddIdentityServer(options =>
{
options.IssuerUri = "http://localhost:54100";
})
.AddInMemoryClients(Clients.Get())
.AddInMemoryScopes(Scopes.Get())
.AddInMemoryUsers(Users.Get())
.SetSigningCredential(cert);
services.AddAuthorization(options =>
{
options.AddPolicy(OrderApiConstants.StoreIdPolicyName, policy => policy.Requirements.Add(new StoreIdRequirement("storeId")));
});
services.AddSingleton<IPersistedGrantStore, InMemoryPersistedGrantStore>();
services.AddSingleton(_orderManagerMock.Object);
services.AddMvc();
}
private void …Run Code Online (Sandbox Code Playgroud) 使用 IdentityServer4,我正在现有系统上实现代码流授权,该系统仅支持 IdentityServer 中的 ResourceOwnerPassword 授权类型并且运行良好。
我进入了一个用户被提升为身份验证的阶段,然后重定向到 server connect/authorize/callback
之后,它不会进行身份验证并返回登录页面。从日志中,我得到以下信息:
调用 IdentityServer 端点:IdentityServer4.Endpoints.AuthorizeCallbackEndpoint for /connect/authorize/callback
验证授权请求
{
"ClientId": "demo",
"ClientName": "MVC Client Demo",
"RedirectUri": "http://localhost:5002/signin-oidc",
"AllowedRedirectUris": ["http://openidclientdemo.com:8001/signin-oidc",
"http://localhost:5002/signin-oidc"],
"SubjectId": "anonymous",
"ResponseType": "code id_token",
"ResponseMode": "form_post",
"GrantType": "hybrid",
"RequestedScopes": "openid profile default-api",
"State": "CfDJ8DU4Xalc7d9HttaFlZ-UaDhGsjeeEccTfNiNZFz3yYZI9MfXjrXo2TAzT9f6-F2VEdC7bWtcD7HRJgkqrh4t0oTAe_47IBfcm5Fsde9bVRorq-CHcL6_c15Vz1G5LJyEzqX6tyjhcZ2g3J2JjxD1PME1W7sq9YSddhQUJmxt4bVi70wdTtXXp0tH0SKyb0vLCs3eIjOln792nobaFOCM7r3VJ8BfXfpDm2pOOmBlR7zCCBxFCivwj7Zmy5Tu8Z09MvzOaLEkPBSL5i9GyxmGiB0P6osBPYEDyoRfgy2qDigH3QqLmLYjfE6NrgcgIGO9kgXuUT52XgALV_ZKjNbih-Y",
"Nonce": "636702233595840569.NGQ3NGVlODMtYTVhNy00MjM4LWFhNGQtNTFiZTE3ZjllZmUzNjU5MGNmNjktNjg3Yy00YmZlLWEwYWYtYmMzM2QxZmZlNjBk",
"Raw": {
"client_id": "demo",
"redirect_uri": "http://localhost:5002/signin-oidc",
"response_type": "code id_token",
"scope": "openid profile default-api",
"response_mode": "form_post",
"nonce": "636702233595840569.NGQ3NGVlODMtYTVhNy00MjM4LWFhNGQtNTFiZTE3ZjllZmUzNjU5MGNmNjktNjg3Yy00YmZlLWEwYWYtYmMzM2QxZmZlNjBk",
"state": "CfDJ8DU4Xalc7d9HttaFlZ-UaDhGsjeeEccTfNiNZFz3yYZI9MfXjrXo2TAzT9f6-F2VEdC7bWtcD7HRJgkqrh4t0oTAe_47IBfcm5Fsde9bVRorq-CHcL6_c15Vz1G5LJyEzqX6tyjhcZ2g3J2JjxD1PME1W7sq9YSddhQUJmxt4bVi70wdTtXXp0tH0SKyb0vLCs3eIjOln792nobaFOCM7r3VJ8BfXfpDm2pOOmBlR7zCCBxFCivwj7Zmy5Tu8Z09MvzOaLEkPBSL5i9GyxmGiB0P6osBPYEDyoRfgy2qDigH3QqLmLYjfE6NrgcgIGO9kgXuUT52XgALV_ZKjNbih-Y",
"x-client-SKU": "ID_NET",
"x-client-ver": "2.1.4.0",
"accessToken": "4155a526-2bb1-4d88-ba3a-21cb3a91f266",
"userId": "MQ=="
}
Run Code Online (Sandbox Code Playgroud)
}
显示登录:用户未通过身份验证
我不清楚是什么导致了身份验证失败,知道吗?
identityserver4 ×10
asp.net-core ×6
c# ×6
oauth-2.0 ×2
openid ×2
.net-core ×1
asp.net-mvc ×1
azure ×1
docker ×1
oauth ×1