diff --git a/Controllers/LoginController.cs b/Controllers/LoginController.cs index 905d02c..b371ea9 100644 --- a/Controllers/LoginController.cs +++ b/Controllers/LoginController.cs @@ -42,7 +42,7 @@ namespace ApiSoftway.Controllers else { _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)) { @@ -89,5 +89,64 @@ namespace ApiSoftway.Controllers } } + [HttpPost("loginMagazzino")] + public async Task> 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 + { + //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); + } + } } } diff --git a/Program.cs b/Program.cs index 6d266fc..00626b3 100644 --- a/Program.cs +++ b/Program.cs @@ -55,5 +55,5 @@ app.UseAuthorization(); app.MapControllers(); -//app.Run(); -app.Run("http://localhost:6000"); +app.Run(); +//app.Run("http://localhost:6000");