我的应用程序和 API 使用 IdentityServer 4 进行保护。
我有一个用于用户管理的集中 API(注册新用户、更新、删除和重置密码)。这个 api 生成的令牌将被 identityserver 用来重置用户的密码。
问题是我总是收到无效令牌错误。我知道这与 url 编码无关,因为忘记的密码由身份服务器处理,并且身份服务器生成的令牌工作正常。问题是当令牌由不同的 api 生成时(即使在一台机器上)。
我考虑创建一个通用的数据保护提供程序,但我不清楚这是如何完成的。基本上,我怎样才能重置由另一个接受的一个 api 创建的密码令牌?
我正在使用 Asp Identity 的用户管理器来生成重置密码令牌:
var token = await _userManager.GeneratePasswordResetTokenAsync(appUser);
Run Code Online (Sandbox Code Playgroud)
这是我的 IdentityServer 被设置为使用 Asp Identity 的方式:
services
.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Lockout.AllowedForNewUsers = true;
options.Lockout.DefaultLockoutTimeSpan = new System.TimeSpan(12, 0, 0);
options.Lockout.MaxFailedAccessAttempts = int.Parse(Configuration["MaxFailedAttempts"]);
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
...
var builder = services.AddIdentityServer(options =>
{
options.Events.RaiseErrorEvents = true;
options.Events.RaiseInformationEvents = true;
options.Events.RaiseFailureEvents = true;
options.Events.RaiseSuccessEvents = true;
options.Authentication.CookieSlidingExpiration = true;
}) …Run Code Online (Sandbox Code Playgroud) 我有一个.Net Core 2.0 Web Api.我有各种具有属性验证属性的模型:
[Required]
public short? Quantity { get; set; }
Run Code Online (Sandbox Code Playgroud)
我有一个ActionFilter来检查模型状态:
if (!context.ModelState.IsValid)
context.Result = new BadRequestObjectResult(context.ModelState);
Run Code Online (Sandbox Code Playgroud)
无论我做什么,当我故意省略所需的属性时,ModelState总是返回有效.我的控制器标记为:
[Produces("application/json")]
Run Code Online (Sandbox Code Playgroud)
模型正在反序列化,我的动作方法中的模型参数标有[FromBody].它似乎没有运行任何验证(标准或自定义).我已经看过这个答案以及这一个和其他几个但我无法弄清楚我错过了什么.我的API受IdenityServer 4保护,因此不确定是否会影响它,但此时我必须自己验证每个动作方法,这不是我想要做的.有人有建议吗?