我正在尝试为 SPA 创建 API。我正在使用最新的 .NET Core、MVC 和 EF。我想使用 JWT 对用户进行身份验证,因此我决定使用 openiddict 核心。我尝试根据github 页面和这篇博客文章中的示例进行设置。问题是我收到“不支持指定的授权类型”。当请求令牌时。
Here's screenshot of postman request:
Here's my ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
);
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddOpenIddict<ApplicationDbContext>()
.UseJsonWebTokens()
.EnableTokenEndpoint("/connect/token")
.AllowPasswordFlow()
.DisableHttpsRequirement() // TODO: remove in production
.AddEphemeralSigningKey(); // TODO: replace with a certificate, this should be used for development only
}
Run Code Online (Sandbox Code Playgroud)
And the Configure
method:
public void Configure(IApplicationBuilder app, IHostingEnvironment …
Run Code Online (Sandbox Code Playgroud) 我有个问题.我今天早上打开了我的项目并得到了错误:
找不到类型或命名空间名称'OpenIddictDbContext <,,>'(您是否缺少using指令或程序集引用?)[netcoreapp1.1]
我恢复并构建项目时发生此错误.这很奇怪,因为我在我的project.json文件中有"OpenIddict":"1.0.0-*",我正在使用引用:using OpenIddict ;
这个问题在我的项目中到处都会出现问题,因为他似乎没有认识到"使用OpenIddict"
如果它有帮助,这是我得到错误的例子(ApplicationDbContext.cs)
namespace Overnight.Db
{
//the error: The type or namespace name 'OpenIddictDbContext<,,>' could not be found (are you missing a using directive or an assembly reference?)
public class ApplicationDbContext : OpenIddictDbContext<ApplicationUser, ApplicationRole, Guid>
{
Run Code Online (Sandbox Code Playgroud)
要么
//the error: 'OpenIddictDbContext<ApplicationUser, ApplicationRole, Guid>' does not contain a constructor that takes 1 arguments
protected override void OnModelCreating(ModelBuilder builder)
{
Run Code Online (Sandbox Code Playgroud)
这是我的project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform", …
Run Code Online (Sandbox Code Playgroud) 我正在使用密码授予流程和 asp.net Identity。
我想在每次执行登录时杀死用户的所有刷新令牌。即使他使用不同的设备(例如其他电脑或智能手机)登录,我也需要它来终止其“会话”。
那么,我该怎么做呢?
我可以只做一个UserManager.UpdateSecurityStampAsync(user.Id);
,还是我需要别的东西?
非常感谢你的帮助!
正如标题所说,得到:
“IDX10609:解密失败。未尝试任何密钥:令牌:'System.String'。”
尝试进行身份验证时出错。使用 Openiddict 作为身份验证服务器。我确信我在其中或 api 服务器中配置了错误,但我不知道是什么。我一直在尝试不同的组合,但目前却陷入困境。这是身份验证服务器配置:
public void ConfigureServices(IServiceCollection services) {
services.AddDbContext<TrustContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("Trust"), b => b.MigrationsAssembly("Application.Trust"));
options.UseOpenIddict();
});
services.AddDefaultIdentity<AspNetUsers>()
.AddEntityFrameworkStores<TrustContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
options.ClaimsIdentity.UserNameClaimType = Claims.Name;
options.ClaimsIdentity.UserIdClaimType = Claims.Subject;
options.ClaimsIdentity.RoleClaimType = Claims.Role;
});
services.AddOpenIddict()
// Register the OpenIddict core components.
.AddCore(options =>
{
options.UseEntityFrameworkCore()
.UseDbContext<TrustContext>();
})
.AddServer(options =>
{
options.IgnoreEndpointPermissions()
.IgnoreGrantTypePermissions()
.IgnoreScopePermissions();
// Enable the authorization, logout, token and userinfo endpoints.
options.SetAuthorizationEndpointUris("/connect/authorize")
.SetLogoutEndpointUris("/connect/logout")
.SetTokenEndpointUris("/connect/token")
.SetUserinfoEndpointUris("/connect/userinfo");
options.RegisterScopes(Scopes.Email, Scopes.Profile, Scopes.Roles, Scopes.OpenId);
options.AllowAuthorizationCodeFlow()
.AllowPasswordFlow()
.AllowImplicitFlow()
.AllowHybridFlow()
.AllowRefreshTokenFlow();
options.AddDevelopmentEncryptionCertificate()
.AddDevelopmentSigningCertificate(); …
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试为我的公司设置一个基于 OpenIddict 的 AuthServer。我目前正在努力从我的一个测试 API 访问安全端点。
从长远来看,我想要实现的目标是:一个基于 OpenIddict 的集中式身份验证服务器,我的同事可以在其中注册他们的 API/客户端/软件项目以进行安全访问。为简单起见,我将同事的 API 之一称为 API1。该 API1 有一堆控制器和端点,需要使用身份验证服务器进行保护。一些端点需要不同的保护级别。因此,API1 的 Client1 仅获取读取端点的凭据,而 Client2 也获取读取和写入端点的凭据。
现在我尝试从一个简单的示例开始,其中我有 Auth-Server 和 API1,客户端现在是 Postman。
我在 Postman 中使用 OAuth2 方法获得了一个有效的令牌,这是 Auth-Server 的日志:
info: OpenIddict.Server.OpenIddictServerDispatcher[0]
The request address matched a server endpoint: Token.
info: OpenIddict.Server.OpenIddictServerDispatcher[0]
The token request was successfully extracted: {
"grant_type": "client_credentials",
"scope": "",
"client_id": "resource_server_1",
"client_secret": "[redacted]"
}.
info: OpenIddict.Server.OpenIddictServerDispatcher[0]
The token request was successfully validated.
info: OpenIddict.Server.OpenIddictServerDispatcher[0]
The response was successfully returned as a JSON …
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以使用openiddict
类似于此处使用Identity Server
.
var result = await _validator.ValidateAccessTokenAsync(userToken);
if (result.IsError)
{
context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant);
return;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何等效的方法来ValidateAccessTokenAsync
验证openiddict
令牌并访问其某些属性?
我正在构建 ASP.NET Core 1.1 应用程序(跨平台)并尝试(使用此示例)access_token
在请求/connect/token
端点时将自定义声明添加到返回的内容中。
我需要的是不仅返回序列化的声明,access_token
而且在响应中返回它们,如下所示:
{
"token_type": "Bearer",
"access_token": "...",
"expires_in": 1799,
"custom_claim": "..."
}
Run Code Online (Sandbox Code Playgroud)
我在互联网上发现我必须使用AspNet.Security.OpenIdConnect.Server
并编写我的提供者才能做我想做的事。
没有使用第一个示例的简单方法吗?
我使用的是 OAUth 2.0,授权类型Password
,没有 JWT。
不要求不使用 JWT,只是我习惯了 ASP.NET 4.5 中的 OAuth
我https
在非本地托管环境上使用 ARR,无需卸载。这意味着命中 ARR 的请求为 ,https
但发送到后端服务器的请求仅为http
。
如果我禁用https
openiddict 中的要求,则配置返回http
url。如果我启用它,那么请求将被拒绝,因为后端服务器将它们接收为http
. 有没有办法解决?
我试图oidc-client
在角度应用程序中使用 with oppeniddict 但存在错误.well-known/openid-configuration
。
错误说:
GET http://localhost:2987/.well-known/openid-configuration 400 (Bad Request)
Run Code Online (Sandbox Code Playgroud)
我在 dot-net core 5 应用程序中有 openiddict 实现。
然后我抓取 URLhttp://localhost:2987/.well-known/openid-configuration
并在浏览器中浏览它,我收到错误:
{
"error": "invalid_request",
"error_description": "This server only accepts HTTPS requests.",
"error_uri": "https://documentation.openiddict.com/errors/ID2083"
}
Run Code Online (Sandbox Code Playgroud)
我还从 Web 服务器设置中禁用了 SSL,如图所示:
我的启动ConfigureServices看起来像这样:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(Configuration["ConnectionString"], sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
});
options.UseOpenIddict();
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
options.ClaimsIdentity.UserNameClaimType = Claims.Name;
options.ClaimsIdentity.UserIdClaimType = Claims.Subject;
options.ClaimsIdentity.RoleClaimType = Claims.Role;
});
services.AddOpenIddict() …
Run Code Online (Sandbox Code Playgroud) 我想使用客户端凭据对 API 资源进行身份验证。
我已经能够成功生成令牌。
在发送 API 请求时,我记录了错误,它显示:
2021-06-10T00:47:19.1953056+05:45 [ERR] (OpenIddict.Validation.OpenIddictValidationDispatcher) The authentication demand was rejected because the token had no audience attached.
2021-06-10T00:47:19.1954307+05:45 [INF] (OpenIddict.Validation.AspNetCore.OpenIddictValidationAspNetCoreHandler) "OpenIddict.Validation.AspNetCore" was not authenticated. Failure message: "An error occurred while authenticating the current request."
2021-06-10T00:47:19.1960031+05:45 [INF] (OpenIddict.Validation.OpenIddictValidationDispatcher) The response was successfully returned as a challenge response: "{
\"error\": \"invalid_token\",
\"error_description\": \"The specified token doesn't contain any audience.\",
\"error_uri\": \"https://documentation.openiddict.com/errors/ID2093\"
}".
2021-06-10T00:47:19.1960852+05:45 [INF] (OpenIddict.Validation.AspNetCore.OpenIddictValidationAspNetCoreHandler) AuthenticationScheme: "OpenIddict.Validation.AspNetCore" was challenged.
Run Code Online (Sandbox Code Playgroud)
我的配置中缺少什么?使用客户端凭据授予类型通过 openiddict 保护 API 资源的正确方法是什么?
资源服务器启动配置: …