我正在尝试使用 MsAdalAngular6Service 对Active Directory用户进行身份验证,该 Azure Active Directory身份验证库使用jwt承载令牌在客户端对用户进行身份验证。< / p>
我的角度代码看起来像
@FXML
public void getNewImageHandler(KeyEvent event) {
if (event.getCode().equals(KeyCode.N)) {
photoSwipeCounter++;
}
if (event.getCode().equals(KeyCode.P)) {
photoSwipeCounter--;
}
// wrap around logic
if (photoSwipeCounter < 0) {
photoSwipeCounter = selectedImgsList.size() - 1;
} else if (photoSwipeCounter >= selectedImgsList.size()) {
photoSwipeCounter = 0;
}
singleFile = selectedImgsList.get(photoSwipeCounter);
image = new Image(singleFile.toURI().toString(),
900, 400,
true, true, true);
imgFieldView.setImage(image);
}
在这里,我使用获取令牌来验证用户身份,并且startup.cs文件代码如下。
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())
}
}
我的控制器动作如下。
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;
});
但是每当我通过angular调用此操作时,它始终会返回未经授权的401错误。
acquire令牌始终返回 UnAutherize 401 ,但是当我传递Access令牌时,它没有显示401,但我只需要传递acquire令牌。 无法找出问题出在哪里。
答案 0 :(得分:0)
确保像这样在控制器中具有AuthenticationSchemes
[Authorize(AuthenticationSchemes = "Bearer")]
[Route("api/[controller]/")]
public class AssetController : Controller
还要确保这一行中有令牌
var token = localStorage.getItem('token');