简体   繁体   English

ASP.NET Core 2.0 Web API 抛出 500 错误并且不允许访问 http 帖子

[英]ASP.NET Core 2.0 Web API throwing 500 error and not allowing access on http post

I've set up my Cors policy in my Startup.cs by first adding the policy to ConfigureServices method:我已经在 Startup.cs 中设置了我的 Cors 策略,首先将策略添加到 ConfigureServices 方法:

 services.AddCors(
            cfg =>
            {
                cfg.AddPolicy("MyPolicy",
                    bldr => {  
                bldr.AllowAnyHeader()
               .AllowAnyMethod()
               .WithOrigins("http://localhost:8001"); });

                cfg.AddPolicy("AnyGET",
                    bldr => { bldr.AllowAnyHeader().WithMethods("GET").AllowAnyOrigin();});
            });

And next I'm using this policy as declared in my Configure method:接下来我将使用在我的 Configure 方法中声明的这个策略:

            app.UseCors("MyPolicy");
            app.UseAuthentication();

           app.UseMvc(config => { config.MapRoute("MyAPIRoute", 
              "api/{controller}/{action}"); });

In My Controller I've Enabled Cors specifying my policy and I've configured a specific Controller method to accept a bearer token as as way authenticating a the http post:在我的控制器中,我已经启用 Cors 来指定我的策略,并且我已经配置了一个特定的控制器方法来接受承载令牌作为验证 http 帖子的方式:

namespace My.Api.Controllers
{
[EnableCors("MyPolicy")]
public class AccountController : Controller
{

.... ....

 [HttpPost]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public async Task<IActionResult> ChangePassword([FromBody] PasswordChangeViewModel model)
    {

From my angular app I am making a http post request to this method:从我的 angular 应用程序中,我正在向此方法发出 http post 请求:

public changePassword(password: Password) {

return this.http.post("http://localhost:8000/api/account/changepassword", password,
  {
    headers: new Headers({"Authorization": "Bearer " + localStorage.getItem('token').toString()})
  }).pipe(map((res: Response) => res.json()));
}

I've tried every suggestion on stack overflow but nothing seems to work and continue to get this error:我已经尝试了有关堆栈溢出的所有建议,但似乎没有任何效果并继续出现此错误:

在此处输入图片说明

I have the same setup for another project and this wasn't ever an issue.我对另一个项目有相同的设置,这从来都不是问题。 The only difference is I updated this angular app to the angular 6 from 5. Also whenever I make this call from Postman its successful.唯一的区别是我将此 angular 应用程序从 5 更新为 angular 6。此外,每当我从 Postman 发出此调用时,它都会成功。 I've hit a wall and need help!我撞墙了,需要帮助! Thanks in advance!提前致谢!

UPDATE I've changed the angular call to:更新我已将角度调用更改为:

public changePassword(password: Password) {

let headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token').toString() });
let options = new RequestOptions({ headers: headers });

return this.http.post("http://localhost:8000/api/account/changepassword", password, options);
}

I'm still getting the same error.我仍然遇到同样的错误。

UPDATE I have also tried to change how my angular service makes the http post by using HttpClient from @angular/common/http:更新我还尝试通过使用来自@angular/common/http 的 HttpClient 来更改我的 angular 服务如何制作 http 帖子:

public changePassword(password: Password) {

let headers = new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem('token').toString() });


return this.httpClient.post("http://localhost:8000/api/account/changepassword", password, { headers: headers} );
}

UPDATE I'm going in circles with this.更新我正在兜圈子。 It seems like I've tried EVERYTHING.好像我什么都试过了。 Please help!!请帮忙!!

I finally figured this out yesterday smh...I'm ashamed but the issue was that one of my Password model properties didn't match the ModelView Password in my API.我昨天终于弄明白了这个问题……我很惭愧,但问题是我的密码模型属性之一与我的 API 中的 ModelView 密码不匹配。 Once they matched this worked fine...一旦他们匹配,这工作正常......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM