我们用来identity server 4保护 api/资源。要求之一是跟踪用户活动,这意味着用户上次使用 api(未登录但使用)的时间。由于我们有 30 多个 API,我们认为一旦令牌根据identity server.
我的问题是,每次用户想要访问 api 时,这种验证真的会在身份服务器级别发生吗?
是否有办法获取此验证时间戳并将其保存在数据库中的某个位置?
谢谢
c# access-token asp.net-web-api2 bearer-token identityserver4
[HttpPost("xxxxxxxxxx")]
[Authorize(Roles = "xxxxxxxxx")]
public IActionResult Post([FromBody]xxxxxxx xxxxxxxxxxxxxx)
{
if (s == null)
{
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是属性Authorize(Roles ="Director")。我的申请角色是学生、总监、经理。这个特定的方法只能由主管访问,并且对于上述方法它给我返回 401 未经授权。
export function xxxxxxxxxxxx(token, formValues) {
return axios.post(`${ROOT_URL}/xxxxxxx/xxxxxxxx`, formValues);
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何通过 axios 调用在标头中发送不记名令牌。
I\xe2\x80\x99m 尝试将承载令牌添加到我的 POST 路由中。当我通过 Postman 提交 POST 请求时,我\xe2\x80\x99m 得到以下输出:
\n\n{\n "success": true,\n "token": "Bearer undefined"\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的 user.js 代码:
\n\nrouter.post("/login", (req, res) => {\n const email = req.body.email;\n const password = req.body.password;\n\n //find user by email\n User.findOne({ email }).then(user => {\n //check for user\n if (!user) {\n return res.status(404).json({ email: "user not found" });\n }\n //check password\n bcrypt.compare(password, user.password).then(isMatch => {\n if (isMatch) {\n //user matched\n const payload = { id: user.id, name: user.name, avatar: user.avatar }; //create …Run Code Online (Sandbox Code Playgroud) 我有一个 ASP.Net Core web api,它接受不记名令牌 (jwt),我在我的 Flutter 中使用 Android 模拟器发送这个令牌,此代码没有任何问题:
final response = await get(url, headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token',
});
Run Code Online (Sandbox Code Playgroud)
但是当我在 Chrome 浏览器中运行它时,api 响应是: 405 Method Not Allowed 。
有什么问题 ?
更新:
根据@anirudh 的评论,我在我的 web Api 中启用了 CROS 并且问题已经改变。现在的响应是:204 No Content
我正在尝试使用 JWT 不记名令牌和谷歌身份验证来实现对 Web API 的身份验证。发现这个答案非常有帮助,但是当它成功验证时,我得到 500,但有以下例外:
System.NullReferenceException:未将对象引用设置为对象的实例。在 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync() Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:错误:执行请求时发生未处理的异常。
System.NullReferenceException:未将对象引用设置为对象的实例。在 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync() 在 Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync() 在 Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync() 在 Microsoft.AspNetCore.Authentication.AuthenticationService .AuthenticateAsync(HttpContext 上下文,字符串方案) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文) 在 Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
当令牌无效时,我会收到 401 响应。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
});
});
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
o.SecurityTokenValidators.Clear();
o.SecurityTokenValidators.Add(
new GoogleTokenValidator(
client_id
));
});
services.AddScoped<PhotoService>();
services.AddScoped<TagService>();
services.AddScoped(_ => new BlobServiceClient(Configuration.GetConnectionString("AzureBlobStorage")));
services.AddDbContext<Data.DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllers().AddJsonOptions(options =>
{ …Run Code Online (Sandbox Code Playgroud) bearer-token ×5
c# ×2
jwt ×2
access-token ×1
asp.net-core ×1
express ×1
flutter ×1
flutter-web ×1
node.js ×1
reactjs ×1
redux ×1