ниже - код из 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);
}
ниже - код действия веб-API dotnetcore 2.1,
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.Есть ли какие-либо недостатки в моих настройках CORS для решения webnet API dotnetcore2.1
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();
}