Advertisement
otkalce

Authentication - email validation

Mar 10th, 2023 (edited)
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | Source Code | 0 0
  1.     // *** Request model ***
  2.     public class ValidateEmailRequest
  3.     {
  4.         public string Username { get; set; }
  5.         public string B64SecToken { get; set; }
  6.     }
  7.  
  8. // *** Service (repository) method implementation ***
  9.         public void ValidateEmail(ValidateEmailRequest request)
  10.         {
  11.             var target = _users.FirstOrDefault(x =>
  12.                 x.Username == request.Username && x.SecurityToken == request.B64SecToken);
  13.  
  14.             if (target == null)
  15.                 throw new InvalidOperationException("Authentication failed");
  16.  
  17.             target.IsConfirmed = true;
  18.         }
  19.  
  20.  
  21. // *** Validate email action implementation (return generated email validation token) ***
  22.         [HttpPost("[action]")]
  23.         public ActionResult ValidateEmail([FromBody] ValidateEmailRequest request)
  24.         {
  25.             try
  26.             {
  27.                 _userRepository.ValidateEmail(request);
  28.                 return Ok();
  29.             }
  30.             catch (InvalidOperationException ex)
  31.             {
  32.                 return BadRequest(ex.Message);
  33.             }
  34.         }
  35.  
  36.  
Tags: jwt-token
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement