【发布时间】:2021-03-09 00:41:14
【问题描述】:
我正在开发一个带有 ASP.NET Core 后端的 Angular Web 应用程序。我的角色有问题。成功登录后,我收到一个包含用户角色的令牌,但是当我尝试调用方法时,我总是从 asp.net 得到这个答案:http://localhost:5000/Account/Login?ReturnUrl=%2Fapi %2FClient%2F 详细信息。为什么?令牌有效且未过期。
角度:
getAllWithContactDetails(): Observable<ClientContact[]> {
console.log(`here: ${localStorage.getItem('token')}`);
return this.http.get<ClientContact[]>(`${links.API_URI}/api/Client/details`, {
headers: new HttpHeaders({
'Accept': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
})
});
}
ASP.NET 核心
//Controller:
[HttpGet("details"), Authorize(Roles = "Admin,Employee,Seller")]
public IEnumerable<ClientContact> GetClientsWithContactDetails()
{
...
}
//Login:
...
var tokeOptions = new JwtSecurityToken(
issuer: jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],
audience: jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],
claims: new List<Claim>() {
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Name, user.UserName),
new Claim(ClaimTypes.Role, _userManager.GetRolesAsync(user).Result.First())
},
expires: DateTime.Now.AddMinutes(10),
signingCredentials: new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256)
);
return Ok(new { Token = new JwtSecurityTokenHandler().WriteToken(tokeOptions) });
ConfigureServices 中的Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddDbContext<WebApiContext>(opt =>
opt.UseSqlServer(Configuration.GetConnectionString("SlkDatabase")));
var jwtAppSettingOptions = Configuration.GetSection(nameof(JwtIssuerOptions));
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtAppSettingOptions[nameof(JwtIssuerOptions.Issuer)],
ValidateAudience = true,
ValidAudience = jwtAppSettingOptions[nameof(JwtIssuerOptions.Audience)],
ValidateIssuerSigningKey = true,
IssuerSigningKey = _signingKey,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
});
services.AddIdentity<User, IdentityRole>(options =>
{
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
options.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<WebApiContext>()
.AddDefaultTokenProviders();
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApi", Version = "v1" });
});
services.AddMvc().AddControllersAsServices();
}
回复:
【问题讨论】:
-
你的意思是“但是当我尝试调用一个方法时,我总是从 asp.net 得到这个答案:”服务器响应重定向到
http://localhost:5000/Account/Login?ReturnUrl=%2Fapi%2FClient%2Fdetails你甚至发出的每个请求如果您已经通过身份验证? -
没错。我不知道它为什么要将我重定向到 /Account/Login 页面(该页面不存在)。登录页面的路径就是 /login。
-
你能分享
Startup.cs的ConfigureServices吗?我怀疑可能使用了AddDefaultIdentity配置,它想要使用默认的 Razor 页面和身份重定向。 (docs.microsoft.com/en-us/dotnet/api/…) -
@Leaky 感谢您等待我与
AddDefaultIdentity一起检查这个想法。 :) 根据更新后的帖子,情况似乎并非如此。请求-响应管道可能无法处理在GetClientsWithContactDetails操作方法中创建的令牌。但我不明白,究竟为什么,所以如果你有一个想法,你可以将它作为答案发布。 :) -
在我看来,重定向仍然存在一些问题,因为 404 似乎与登录端点有关,即使原始请求被发送到不同的端点。我认为一个问题是,在调用
AddEntityFrameworkStores()之前,我看不到使用AddRoles<IdentityRole>()添加的角色支持,并且据我记得在使用AddIdentity()时需要这样做。另外,我遇到了ClaimTypes.NameIdentifier的一些奇怪的转换问题,所以我怀疑应该改用JwtRegisteredClaimNames.Sub。但我没有任何具体的东西。 ://
标签: angular asp.net-core roles