[英]Jwt Authorization .NET Core 2.0 with angular always return Unauthorized (HTTP 401)
I am trying to autherize the Active Directory user by using using MsAdalAngular6Service that is Azure Active Directory Authentication Library which authenticate user at client side with using jwt bearer token. 我正在尝试使用MsAdalAngular6Service来自动化Active Directory用户,该服务是Azure Active Directory身份验证库 ,它使用jwt bearer token对客户端的用户进行身份验证。
My angular code looks a like 我的角度代码看起来像
constructor(private adalSvc: MsAdalAngular6Service) {
var token = this.adalSvc.acquireToken('graphApiUri').subscribe((token) => {
localStorage.setItem('token', token);
});
}
GetUser() {
let httpOptions = {
headers: new HttpHeaders({
'Authorization': 'Bearer ' + localStorage.getItem('token'),
})
}
var token = localStorage.getItem('token');
return this._http.get(this.myAppUrl + 'api/Member/getuser', httpOptions)
.map((res: Response) => res.json())
}
}
here I am using the acquire token to authenticate the user and my startup.cs file code is as below. 这里我使用获取令牌来验证用户,我的startup.cs文件代码如下所示。
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = "https://login.microsoftonline.com/6fa7661f-b743-4046-b913-06c1c48eb54e";
//options.Audience = "https://graph.microsoft.com";
options.TokenValidationParameters.ValidateLifetime = true;
options.TokenValidationParameters.ClockSkew = TimeSpan.Zero;
});
and my controller action is as below. 我的控制器动作如下。
[Authorize]
[HttpGet]
[Route("getuser")]
public async Task<User> GetUserInfoAsync()
{
var token = Request.Headers["Authorization"].ToString();
}
but whenever I am calling this action through angular it alway returns the unautherized 401 error. 但每当我通过角度调用此动作时,它总是返回未经过处理的401错误。
the acquire token always returns the UnAutherize 401 but when I am passing Access token it is not showing 401 but i need to pass acquire token only. 获取令牌总是返回UnAutherize 401,但是当我通过Access令牌时,它没有显示401,但我只需要传递获取令牌。 not able to figure out where am going wrong.
无法弄清楚哪里出错了。
Make sure you have AuthenticationSchemes in your controller like this 确保您的控制器中有AuthenticationSchemes,如下所示
[Authorize(AuthenticationSchemes = "Bearer")]
[Route("api/[controller]/")]
public class AssetController : Controller
Also make sure you have the token in this line 还要确保在此行中有令牌
var token = localStorage.getItem('token');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.