Слушайте, ребята, у меня какое-то время такая проблема, и я не знаю, что делать дальше.
У меня есть страница сброса пароля, которая называется Razor Page:
@page
@model Project.API.Pages.ResetarSenhaModel
@{
Layout = null;
var sucesso = Request.Query["sucesso"].FirstOrDefault();
var mensagem = Request.Query["mensagem"].FirstOrDefault();
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Reset Password</title>
</head>
<body>
@if (mensagem == null)
{
<h1>Reset your password</h1>
<form action="Auth/ResetPassword" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" name="Token" value="@Request.Query["Token"]" />
<input type="hidden" name="Email" value="@Request.Query["Email"]" />
<table>
<tr>
<td>
Senha
</td>
<td>
<input type="password" name="NovaSenha" />
<span asp-validation-for="NovaSenha" class="text-danger"></span>
</td>
</tr>
<tr>
<td>
Confirmar Senha
</td>
<td>
<input type="password" name="ConfirmarNovaSenha" />
<span asp-validation-for="ConfirmarNovaSenha" class="text-danger"></span>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Reset" />
</td>
</tr>
</table>
</form>
}
else
{
if (sucesso.Equals("True"))
{
<h1>@mensagem</h1>
}
if (sucesso.Equals("False"))
{
<h1>@mensagem</h1>
}
}
</body>
</html>
вызывает это действие при отправке:
[HttpPost]
public async Task<IActionResult> ResetPassword([FromForm]ResetarSenhaVM resetarSenhaVM)
{
try
{
if (ModelState.IsValid)
{
var resultado = await _autenticacaoService.ResetarSenha(resetarSenhaVM);
if (resultado.sucesso)
return RedirectToPage("/ResetarSenha", new {
sucesso = resultado.sucesso,
mensagem = resultado.mensagem});
return RedirectToPage("/ResetarSenha", new {
sucesso = resultado.sucesso,
mensagem = resultado.mensagem});
}
return RedirectToPage("/ResetarSenha");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
Затем, когда я отправляю форму, я не могу получить сообщения об ошибках атрибута проверки в тегах span, вместо этого я получаю json:
{
"errors": {
"NovaSenha": [
"The NovaSenha field is required.",
"Password length is between 6 and 50."
]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "0HLJIO56EGJEV:00000001"
}
Что я пропал? Любое предложение может помочь, спасибо!
Мой startup.cs:
public class Startup
{
public static IConfiguration Configuration { get; set; }
public static IHostingEnvironment HostingEnvironment { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<PersisteContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddAutoMapper(typeof(MappingProfile));
RepositoryScopeInjector.Add(services);
ServiceScopeInjector.Add(services);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<ILogger>(LogManager.GetCurrentClassLogger());
services.AddCors(options =>
{
options.AddPolicy("AllowMyOrigin",
builder => builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = Configuration["JwtIssuer"],
ValidAudience = Configuration["JwtIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"])),
ClockSkew = TimeSpan.Zero
};
cfg.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
// Add the access_token as a claim, as we may actually need it
var accessToken = context.SecurityToken as JwtSecurityToken;
if (accessToken != null)
{
ClaimsIdentity identity = context.Principal.Identity as ClaimsIdentity;
if (identity != null)
{
identity.AddClaim(new Claim(ClaimTypes.Role, accessToken.Claims.Where(c => c.Type == ClaimTypes.Role).First().Value));
identity.AddClaim(new Claim("id", accessToken.Claims.Where(c => c.Type == "id").First().Value));
identity.AddClaim(new Claim("nome", accessToken.Claims.Where(c => c.Type == "nome").First().Value));
identity.AddClaim(new Claim("email", accessToken.Claims.Where(c => c.Type == "email").First().Value));
}
}
return Task.CompletedTask;
}
};
});
services.AddApiVersioning(options =>
{
options.UseApiBehavior = false;
options.ReportApiVersions = true;
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ApiVersionReader = ApiVersionReader.Combine(
new HeaderApiVersionReader("x-api-version"));
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
if (!HostingEnvironment.IsProduction())
{
services.AddSwaggerGen();
services.ConfigureOptions<ConfigureSwaggerOptions>();
}
#if !DEBUG
services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddAuthorization()
.AddJsonFormatters()
.AddApiExplorer();
#else
services.AddMvcCore(opts =>
{
opts.Filters.Add(new AllowAnonymousFilter());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonFormatters()
.AddApiExplorer();
#endif
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/Home";
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IApiVersionDescriptionProvider apiVersionDescriptionProvider)
{
//Usa appsetings.json de acordo com o ambiente de desenv. que está selecionado
var builder = new ConfigurationBuilder()
.SetBasePath(HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{HostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
app.UseCors("AllowMyOrigin");
var usCulture = new CultureInfo("en-US");
var supportedCultures = new[] { usCulture };
app.UseMiddleware(typeof(ErrorHandlingMiddleware));
app.UseHttpContext();
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(usCulture),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "")),
RequestPath = "",
});
app.UseAuthentication();
if (!HostingEnvironment.IsProduction())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
c.SwaggerEndpoint($"../swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
c.RoutePrefix = "bmdoc";
});
}
app.UseMvc();
}
}
}