*Joan* Aggiunto su controller login metodo loginMagazzino

This commit is contained in:
polo 2024-06-18 17:10:35 +02:00
parent 94e8599311
commit 035b081ff2
2 changed files with 62 additions and 3 deletions

View File

@ -42,7 +42,7 @@ namespace ApiSoftway.Controllers
else else
{ {
_personale = _personale_context.Personale; _personale = _personale_context.Personale;
var pers=await _personale.Where(t=> t.Tcuser!=null && t.Tcuser.Equals(model.Username)&& t.Tcpwd!=null && t.Tcpwd.Equals(model.Password)).Take(1).ToListAsync(); var pers = await _personale.Where(t => t.Tcuser != null && t.Tcuser.Equals(model.Username) && t.Tcpwd != null && t.Tcpwd.Equals(model.Password)).Take(1).ToListAsync();
if (pers == null || (pers != null && pers.Count == 0)) if (pers == null || (pers != null && pers.Count == 0))
{ {
@ -89,5 +89,64 @@ namespace ApiSoftway.Controllers
} }
} }
[HttpPost("loginMagazzino")]
public async Task<ActionResult<Login_out>> loginMagazzino([FromBody] Login model)
{
Login_out o = new Login_out();
try
{
if (string.IsNullOrEmpty(model.Username) || string.IsNullOrEmpty(model.Password))
{
o.err_detail = "Username e Password non possono essere vuoti.";
o.err_title = "Username e Password non possono essere vuoti.";
o.err_status_code = "200";
return StatusCode(StatusCodes.Status200OK, o);
}
_personale = _personale_context.Personale;
var pers = await _personale.Where(t => t.Tcuser != null && t.Tcuser.Equals(model.Username) && t.Tcpwd != null && t.Tcpwd.Equals(model.Password) && t.Tcruolo != null && t.Tcruolo.Equals("MAG")).Take(1).ToListAsync();
if (pers == null || (pers != null && pers.Count == 0))
{
o.err_detail = "Username o Password non trovati.";
o.err_title = "Username o Password non trovati.";
o.err_status_code = "404";
return StatusCode(StatusCodes.Status404NotFound, o);
}
else
{
var authClaims = new List<Claim>
{
//new Claim(ClaimTypes.Name, model.Username),
new Claim(ClaimTypes.Name,pers.First().Tcuser),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim("user", pers.First().Tcuser),
new Claim("codice", pers.First().Catcodice),
new Claim("nome", pers.First().Catnome),
};
var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));
var token = new JwtSecurityToken(
issuer: _configuration["JWT:ValidIssuer"],
audience: _configuration["JWT:ValidAudience"],
expires: DateTime.Now.AddMonths(3),
claims: authClaims,
signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
);
string tok = new JwtSecurityTokenHandler().WriteToken(token);
o.Tok = tok;
o.Tccodice = pers.First().Catcodice;
o.Tcruolo = pers.First().Tcruolo;
o.Tcdescri = pers.First().Catnome;
return StatusCode(StatusCodes.Status200OK, o);
}
}
catch (Exception ex)
{
string err = "Errore: " + ex.Message;
o.err_detail = err;
o.err_title = err;
o.err_status_code = "200";
return StatusCode(StatusCodes.Status500InternalServerError, o);
}
}
} }
} }

View File

@ -55,5 +55,5 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
//app.Run(); app.Run();
app.Run("http://localhost:6000"); //app.Run("http://localhost:6000");