不受支持的415型媒体,但在Postman中工作正常

问题描述 投票:0回答:1

注册后,用户将获得一个链接,该链接由userId和令牌组成。当用户单击它时-angular项目打开,然后angular从链接中获取userId和令牌,然后将post方法发送到后端以验证电子邮件。

链接示例

http://localhost:4200/user/confirmemail/?userId=9bc9a474-d10c-438b-8953-1c32fc57551b&token=Q2ZESjhPRkF6d3BPQ1E5TmtLbWplTnlIZ3g3bzJEVEZQZDQ3MFNSblExaWxBTWh3MmdSMWl2ZkU3djZxcWR3bmY4OFMwd2l6STZOY3VMR2FoZ1ZCM28rWFo1YlJhejhOTE5pamFFdGRETTNiNGttT0lOQ2dZVmdLRVlRdWlKRmtQMVpEWHE0b2t2NVBQZTZ0bkR3cUdrb3JiMWRIQUpRUE5pMTZjTW96YUdJcVZBUUxPSG5pd3NDU3BDeTBNREMvMTVyTlhUNUpCL2tRZTJWMjJlTzVHZ1dDbEh5VWNESGNsNlVTQkpXZ1FJaThDTk1kcUovcmdtV0ZEdEQza2hXR1p6V0pEdz09

发布方法,用于验证电子邮件:

        [HttpPost("confirmEmail/{userId}")]
        public async Task<IActionResult> ConfirmEmail(string userId, [FromBody]string token)
        {
            var codeDecodedBytes = WebEncoders.Base64UrlDecode(token);
            var codeDecoded = Encoding.UTF8.GetString(codeDecodedBytes);
            var user = await _userManager.FindByIdAsync(userId);
            var result = await _userManager.ConfirmEmailAsync(user, codeDecoded);
            return Ok(result);
        }

在邮递员中效果很好:enter image description here

Angular项目中的错误:enter image description here

在Angular中获取userId和令牌

  userId: string;
  token: string;
  constructor(private activatedRoute: ActivatedRoute, private authService: AuthService) {
    this.activatedRoute.queryParams.subscribe(params => {
          this.userId = params['userId'];
          this.token = params['token'];
      });
  }

  ngOnInit(): void {
    this.confirmEmail();
  }

  confirmEmail(){
    this.authService.confirmEmail(this.userId, this.token).subscribe(data => { console.log("success")});
  }

((AuthService)将数据发送到后端

  confirmEmail(userId: string, token: string){
    console.log(userId);
    console.log(token);
    return this.http.post(this.authUrl + `confirmemail/${userId}`, token);
  }
c# angular asp.net-core
1个回答
0
投票

尝试将Content-Type设置为json类型:

confirmEmail(userId: string, token: string) {
    const body = JSON.stringify({token});
    const options = {
      headers: new HttpHeaders().append('Content-Type', 'application/json')
    };

    return this.http.post(this.authUrl + `confirmemail/${userId}`, body, options)
          .pipe(
              map(res => res)
            , catchError(this.handleError())
          );
}

0
投票

由于代表,我似乎还无法发表评论,但您的邮递员路线似乎使用了与Angular代码不同的localhost端口。您的Angular代码是否正确插入端口?

© www.soinside.com 2019 - 2024. All rights reserved.