简体   繁体   English

具有角度的Jwt授权.NET Core 2.0始终返回未授权(HTTP 401)

[英]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.

相关问题 HTTP GET 有效,但其他 HTTP 方法返回 401 - Angular 9 Asp.Net Core 3.1 JWT 授权 - HTTP GET works, but other HTTP methods return 401 - Angular 9 Asp.Net Core 3.1 JWT authorization Angular JWT Auth - 总是 401 未经授权 - Angular JWT Auth - always 401 unauthorized 带有ASP.NET Core 2.0的Angular 4在JWT(CORS)上返回了401错误 - Angular 4 with ASP.NET Core 2.0 is giving back 401 error on JWT (CORS) Angular POST and .NET Core WEB Api - 401 Unauthorized error / CORS - Angular POST and .NET Core WEB Api - 401 Unauthorized error / CORS 使用JWT的Angular Detect 401未经授权的访问 - Angular Detect 401 Unauthorized Access using JWT 如何在Angular 2.0中进行JWT授权 - How to JWT Authorization in Angular 2.0 ASP.NET Core 3.1 Web API 中的“授权失败。AuthenticationScheme:AzureADJwtBearer 受到挑战” - 401 Unauthorized - "Authorization failed. AuthenticationScheme: AzureADJwtBearer was challenged" in ASP.NET Core 3.1 Web API - 401 Unauthorized 具有Windows Auth的.Net Core 2.0 REST API-使用401未经授权进行预检 - .Net Core 2.0 REST API w/ Windows Auth - Preflight responding with 401 Unauthorized .NET 核心 JWT 授权失败? - .NET core JWT authorization failed? Angular GET http:// localhost / 401(未经授权)错误 - Angular GET http://localhost/ 401 (Unauthorized) error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM