【发布时间】:2019-09-16 19:50:44
【问题描述】:
我有 Angular 7 前端项目和 asp.net 核心 Web API。
从 Web API 创建 JWT Web 令牌后,我返回到fronted,它将保存在本地存储中。
在我想向 Web API 发送请求后,我会将 JWT Web 令牌放入请求标头部分。
这样就可以了。所以我想使用 JWT 有效负载数据对请求进行身份验证。我的 JWT 有效负载数据有记录用户名、用户角色的一些信息。
我想在通过 http get 请求获取产品详细信息时检查它的有效令牌。你能帮我在 asp.net core web api 中进行身份验证吗?
asp.net 核心 web api,Angular 7 cli
Startup.cs - WEB API
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
services.AddMvc();
构建 Web 令牌 - WEB API
private string BuildToken(MYWebApi.Models.CustomerModel user)
{
var claims = new[] {
new Claim(JwtRegisteredClaimNames.NameId,user.CusId.ToString()),
new Claim(JwtRegisteredClaimNames.Sub,user.CusName),
new Claim(JwtRegisteredClaimNames.Email,user.CusEmail),
new Claim("role","user"),
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(_config["Jwt:Issuer"],
_config["Jwt:Issuer"],
claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds);
return new JwtSecurityTokenHandler().WriteToken(token);
}
将令牌放入标题部分 - FRONT END
@Injectable( )
export class TokenInterceptorService implements HttpInterceptor{
constructor(private injector:Injector) { }
intercept(req, next){
let serverService = this.injector.get(ServerService)
let tokenizedReq = req.clone({
setHeaders:{
Autherization:`Bearer ${serverService.getToken()}`
}
})
return next.handle(tokenizedReq)
}
}
控制器 - WEB API
[Route("GetProduct")]
[HttpGet]
public List<ProductModel> GetProduct(int productId)
{
var repo = new MEData.Repository.ProductRepo();
var productData = repo.GetProduct(productId);
return productData;
}
【问题讨论】:
-
你似乎已经设置好了,据我所知,你所需要的只是控制器或操作方法上的 Authorize 属性
-
我试过了。但随后它将所有请求显示为 401(未经授权)请求。
-
理想情况下,这就是您想要的行为,以保护您的 api。但是,如果您只有某些需要授权的操作,则只需使用 Authorize 属性标记这些操作
-
你有什么日志?
-
我没有得到你的想法朋友。你是什么意思'日志'?
标签: asp.net angular asp.net-web-api asp.net-core jwt