I'm doing an Api Rest using asp net core 2.1 with Vue, in which I need to get the user's data that I started session. The problem is that when I enter the browser, httpContext.User does not get the data. But when I try it with Postman, he gets it. Both wear the tocken at the request.
This is my driver's code
if (!ModelState.IsValid)
return BadRequest(ModelState);
if (id != customers.Id)
return BadRequest();
DateTime dt = DateTime.Now;
try
{
var user = await _userManager.GetUserAsync(HttpContext.User);
customers.UpdatedAt = dt;
customers.UpdatedBy = user.Email;
_context.Entry(customers).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
Startup.cs code
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("defaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 5;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "yourdomain.com",
ValidAudience = "yourdomain.com", IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(Configuration["Llave_secreta"])),
ClockSkew = TimeSpan.Zero
});
services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddCors();
services.AddMvc().AddJsonOptions(ConfigureJson);
services.AddMvc();
services.AddSession();
// services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext context)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
);
app.UseSession();
app.UseAuthentication();
app.UseMvc();
}