'404 page not found Core Controller constructor doesn't work and the rest
Hello everyone, please help me figure it out - here is the startup.cs
I try to simulate JWT token generatuin.All routings checked and are present.Services too
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
//добавляет контроллеры как зависимости
services.AddControllers();
services.AddRazorPages();
var authOptionConfiguration = Configuration.GetSection("Auth");
//регистрирует данную конфигурацию в контроллерах как зависимость
services.Configure<AuthOptions>(authOptionConfiguration);
services.AddMvc();
services.AddCors(options =>
options.AddDefaultPolicy(
builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
})
);
;
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseStatusCodePages();
app.UseStaticFiles();
}
}
And this is the code of the class controller itself:
This controller actions and its constructor should be executed but in fact no
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly IOptions<AuthOptions> authOptions;
public AuthController(IOptions<AuthOptions> authOptions)
{
this.authOptions = authOptions;
}
private List<Account> Accounts => new List<Account>
{
new Account()
{
Id = Guid.Parse("6DC42CF2-637D-45B1-9CFF-271859151B8B"),
Email = "[email protected]",
Password = "user",
Roles = new Role[]{Role.User}
},
new Account()
{
Id = Guid.Parse("669DDDD8-3803-4E92-BC08-EBA80DEF35A9"),
Email = "[email protected]",
Password = "user2",
Roles = new Role[]{Role.User}
},
new Account()
{
Id = Guid.Parse("9E0170A7-47CC-4FA3-B7D8-6059E0980B70"),
Email = "[email protected]",
Password = "admin",
Roles = new Role[]{Role.Admin}
}
};
[Route("login")]
[HttpPost("login")]
[Produces("application/json")]
public IActionResult Login([FromBody] Login request)
{
var user = AuthenticateUser(request.Email, request.Password);
if (user != null)
{
//Generate JWT
var token = GenerateJWT(user);
return Ok(
new
{
access_token = token
}
);
}
return Unathorized();
}
private IActionResult Unathorized()
{
throw new NotImplementedException();
}
private Account AuthenticateUser(string email, string password)
{
return Accounts.SingleOrDefault(zx => zx.Email == email && zx.Password == password);
}
private string GenerateJWT(Account user)
{
//Header
var authParams = authOptions.Value;
var securityKey = authParams.GetSymmetricSecurityKey();
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
//Header
//PayLoad
var claims = new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Email, user.Email),
new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString())
};
foreach (var role in user.Roles)
{
claims.Add(new Claim("role", role.ToString()));
}
var token = new JwtSecurityToken(authParams.Issuer,
authParams.Audience,
claims,
expires: DateTime.Now.AddSeconds(authParams.TokenLifetime),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}
}
In fact, I get 404 Page Not Found
Status Code: 404; Not Found
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
