C# .NET/Angular应用程序中的JWT令牌身份验证无效
我在验证用户时遇到问题。登录工作正常,我从API收到一个令牌。我将其保存在Angular应用程序中的JwtTokenService中,在执行请求(例如删除)时,我添加了带有值“Bearer token”的“Authorization”头,就像我之前在PostMan中所做的那样。客户要求: 我得到302状态码和重定向到帐户/登录,即使我没有这样的路由 控制台中的错误: 访问位于“”的XMLHttpRequesthttps://localhost:44332/api/car/2“从 起源'http://localhost:4200'已被CORS策略阻止:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源 但是get(具有[AllowAnonymous]属性的get工作正常) 《邮递员》中的请求很有效。我想是我的饼干弄乱了。 .Net配置文件:C# .NET/Angular应用程序中的JWT令牌身份验证无效,c#,.net,angular,typescript,C#,.net,Angular,Typescript,我在验证用户时遇到问题。登录工作正常,我从API收到一个令牌。我将其保存在Angular应用程序中的JwtTokenService中,在执行请求(例如删除)时,我添加了带有值“Bearer token”的“Authorization”头,就像我之前在PostMan中所做的那样。客户要求: 我得到302状态码和重定向到帐户/登录,即使我没有这样的路由 控制台中的错误: 访问位于“”的XMLHttpRequesthttps://localhost:44332/api/car/2“从 起源'htt
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ExceptionHandlingFilter]
public class CarController : ControllerBase
{
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
}
编辑1:
[HttpDelete("{id}")]
[Authorize(Policy = JwtBearerDefaults.AuthenticationScheme)]
public async Task<IActionResult> Delete(int id)
{
[HttpDelete(“{id}”)]
[授权(策略=jwtbearderdefaults.AuthenticationScheme)]
公共异步任务删除(int-id)
{
错误:System.InvalidOperationException:未找到名为“承载者”的授权策略
控制台错误:
访问位于“”的XMLHttpRequesthttps://localhost:44332/api/car/2“起源”http://localhost:4200'已被CORS策略阻止:请求的资源上不存在'Access Control Allow Origin'标头。
zone evergreen.js:2845删除https://localhost:44332/api/car/2 net::ERR_FAILED以下是我如何实现这一点的示例。
服务器
将其添加到启动文件中。
services.AddCors(options =>
{
options.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyHeader();
builder.AllowAnyMethod();
});
});
将其添加到控制器文件中。
[ApiController]
[路线(“[控制器]”)]
//
[Microsoft.AspNetCore.Cors.EnableCors(“MyPolicy”)]
//
公共类WeatherForecastController:ControllerBase
{
[HttpPost]
[路线(“后请求”)]
public IActionResult PostRequest(字符串参数)
{
返回Ok(新的{result=parameter});
}
}
客户端
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with
ASP.NET Core</a>.</p>
</div>
<script
src="https://code.jquery.com/jquery-3.5.1.js"
integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
crossorigin="anonymous"></script>
<script>
$(document).ready(() =>{
$.ajax({
type: 'POST',
url : 'https://localhost:44304/weatherforecast/PostRequest?parameter=someValue',
contentType: 'application/json',
success: function (data){
console.log(data);
},
error: function (data){
console.log(data);
},
});
});
</script>
@{
ViewData[“Title”]=“主页”;
}
欢迎
了解
$(文档).ready(()=>{
$.ajax({
键入:“POST”,
网址:'https://localhost:44304/weatherforecast/PostRequest?parameter=someValue',
contentType:'应用程序/json',
成功:功能(数据){
控制台日志(数据);
},
错误:函数(数据){
控制台日志(数据);
},
});
});
这应该允许CORS。
如果我从控制器中删除
[Microsoft.AspNetCore.Cors.EnableCors(“MyPolicy”)]
,它将失败
您需要的所有信息都可以在这里找到,添加项目应该不会太困难。
您必须在Confugure中使用启动服务:
services.AddCors(options =>
{
options.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyHeader();
builder.AllowAnyMethod();
});
});
并在启动文件中添加要配置的策略名称:
app.UseCors("MyPolicy");
只有当您使用.AddDefaultPolicy(…)时,才可以使用没有策略名称的App.UseCors()。我认为您可能需要查看CORS,我可以看到您已将其添加到代码中,但是您可能还需要添加
[EnableCors(“SomePolicy”)]
对于您的控制器,也据我所知,邮递员并不关心您调用的app.UseCors(…)
在UseRouting
之后,但在UseAuthorization
之前,我修复了Cors一点-我不再收到控制台错误。现在我收到302状态代码并重定向到帐户/登录。如果问题是Cors-我将无法执行GET请求。是的,我安装了标识包来存储用户帐户,但我设置了default对jwt进行身份验证和质询方案。此“帐户/登录”似乎是身份信息尝试将[Authorize]
替换为[Authorize(Policy=JwtBearerDefaults.AuthenticationScheme)]
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with
ASP.NET Core</a>.</p>
</div>
<script
src="https://code.jquery.com/jquery-3.5.1.js"
integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
crossorigin="anonymous"></script>
<script>
$(document).ready(() =>{
$.ajax({
type: 'POST',
url : 'https://localhost:44304/weatherforecast/PostRequest?parameter=someValue',
contentType: 'application/json',
success: function (data){
console.log(data);
},
error: function (data){
console.log(data);
},
});
});
</script>
services.AddCors(options =>
{
options.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyHeader();
builder.AllowAnyMethod();
});
});
app.UseCors("MyPolicy");