【发布时间】:2019-05-17 04:24:07
【问题描述】:
下面是 login.component.ts 的代码,
login() {
const val = this.form.value;
if (val.email && val.password) {
this.authService.login(val.email, val.password)
.subscribe(
data => {
if (data && data.Token) {
// store user details and jwt token in local storage to keep user logged in
//between page refreshes
localStorage.setItem('currentUser', JSON.stringify(data));
console.log("user logged in");
this.router.navigate([this.returnUrl]);
} else {
console.log("user not logged in");
}
},
error => {
this.error = error;
});
}
}
下面是 Angular 服务的代码,
login(email: string, password: string) {
return this.http.post<User>(this.baseUrl + "Authenticate", { email,
password }, httpOptions);
}
下面是dotnetcore 2.1 web api action的代码,
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using API.Utilities;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace API.Controllers
{
[Authorize]
[Produces("application/json")]
[Route("api/[controller]")]
public class UsersController : BaseController
{
private readonly AppSettings _appSettings;
public UsersController(IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}
[AllowAnonymous]
[HttpPost]
[Route("Authenticate")]
public IActionResult Authenticate([FromBody]User userParam)
{
var user = Authenticate(userParam.Email, userParam.Password);
if (user == null)
return BadRequest(new { message = "Username or password is incorrect" });
return Ok(user);
}
public User Authenticate(string username, string password)
{
//////code goes
return user;
}
}
}
}
在提琴手中,我总是可以看到长度为 -1 的帖子请求。不知道是什么问题有什么帮助?
以下来自于 startup.cs。我的 dotnetcore2.1 WEB API 解决方案的 CORS 设置是否有任何缺陷
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).
AddJsonOptions(options => {
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
services.AddDistributedMemoryCache();
services.AddSession(options => {
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(36000);
options.Cookie.HttpOnly = true;
});
services.AddCors(options => {
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseStaticFiles();
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseSession();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc();
}
【问题讨论】:
-
你的控制器/动作方法是否受到 Angular 的影响?
-
从 Angular 调用时,我无法命中 WEB API 控制器动作断点。
-
对于这个请求,它似乎正在处理
CORS,您是否在Starup.cs中启用了cors,例如` app.UseCors(opt => { opt.AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin( ) .AllowCredentials(); }); ` -
是的,我已启用 CORS。但似乎未应用,因为有时在控制台中我可以看到 cors 被阻止,但该错误仅在一段时间后出现
-
app.UseHttpsRedirection();可能会导致问题。尝试删除它
标签: c# angular asp.net-core-webapi angular7 asp.net-core-2.1