Example #1
0
        public async Task <IActionResult> Post(RegistrationModel model)
        {
            try
            {
                var user = await this.context.RegisterUsers.FirstOrDefaultAsync(a => a.UserName == model.UserName);

                if (user == null)
                {
                    model.UserId   = Guid.NewGuid().ToString();
                    model.UserType = 0;
                    model.Password = EncryptDecryptPassword.Encrypt(model.Password, model.UserId.ToString());
                    this.context.RegisterUsers.Add(model);
                    await this.context.SaveChangesAsync();

                    return(Ok(model));
                }
                else
                {
                    return(BadRequest("This User is already registered with us!!!"));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Example #2
0
        public async Task <IActionResult> Login(LoginModel model)
        {
            try
            {
                var user = await this.context.RegisterUsers.FirstOrDefaultAsync(a => a.UserName == model.UserName);

                if (user != null)
                {
                    var testing  = EncryptDecryptPassword.Encrypt(model.Password, user.UserId);
                    var password = EncryptDecryptPassword.Decrypt(user.Password, user.UserId);
                    if (password == model.Password)
                    {
                        var tokenDescriptor = new SecurityTokenDescriptor
                        {
                            Subject = new ClaimsIdentity(new Claim[]
                            {
                                new Claim("UserID", user.UserId.ToString())
                            }),
                            Expires            = DateTime.UtcNow.AddDays(1),
                            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_appSettings.JWT_Secret)), SecurityAlgorithms.HmacSha256Signature)
                        };
                        var tokenHandler  = new JwtSecurityTokenHandler();
                        var securityToken = tokenHandler.CreateToken(tokenDescriptor);
                        var SecurityToken = tokenHandler.WriteToken(securityToken);
                        return(Ok(new { SecurityToken, user }));
                    }
                    else
                    {
                        return(BadRequest("Password is incorrect"));
                    }
                }
                else
                {
                    return(BadRequest("Username is incorrect."));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest("Incorrect username or password"));
            }
        }