public async Task Register([FromBody] AdminCreateDto administrator)
        {
            _logger.LogInfo("API HttpPost api/Account/register");

            try
            {
                var rawPassword = administrator.PasswordHash;
                administrator.PasswordHash = GetHashFromString(administrator.PasswordHash);
                administratorService.AddAdministrator(administrator);
                var identity   = GetIdentity(administrator.Username, rawPassword);
                var encodedJwt = GetToken(administrator.Username, rawPassword);

                var response = new
                {
                    access_token = encodedJwt,
                    username     = identity.Name,
                    role         = identity.Claims.ToList()[1].Value
                };

                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonConvert.SerializeObject(response, new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }));
            }
            catch (Exception e)
            {
                _logger.LogError("API HttpPost api/Account/register " + e.Message);
                await Response.WriteAsync(JsonConvert.SerializeObject(new { error = e.Message }, new JsonSerializerSettings {
                    Formatting = Formatting.Indented
                }));
            }
        }
Exemplo n.º 2
0
 public bool Post([FromBody] AdministratorDTO administrator)
 {
     _adminService.AddAdministrator(administrator);
     return(true);
 }