Exemple #1
0
/*        [Authorize]
 *      [HttpDelete]
 *      [Route("Role")]
 *      public async Task<IActionResult> DeleteRole()
 *      {
 *          try
 *          {
 *
 *              int claimId = Convert.ToInt32(HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Id").Value);
 *
 *              string Role = (HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Role").Value).ToString();
 *
 *              var responseUser = await adminBL.DeleteRole(Role,claimId);
 *
 *              if (!responseUser.Equals(null))
 *              {
 *
 *                  bool Success = true;
 *                  var Message = "Role Insert Successfull";
 *                  return Ok(new { Success, Message, });
 *              }
 *              else
 *              {
 *                  bool Success = false;
 *                  var Message = "Role Insert UnSuccessfull";
 *                  return Conflict(new { Success, Message });
 *              }
 *          }
 *          catch (Exception exception)
 *          {
 *              bool Success = false;
 *              return BadRequest(new { Success, Message = exception.Message });
 *          }
 *      }*/

        /* [Authorize]
         * [HttpGet]
         * [Route("Role")]
         * public async Task<IActionResult> GetAllUser()
         * {
         *   try
         *   {
         *
         *       string Role = (HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Role").Value);
         *
         *       string Email = (HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Email").Value).ToString();
         *
         *       var responseUser = await adminBL.GetAllUser(Email, Role);
         *
         *       if (!responseUser.Equals(null))
         *       {
         *
         *           bool Success = true;
         *           var Message = "Role Insert Successfull";
         *           return Ok(new { Success, Message, Data = responseUser });
         *       }
         *       else
         *       {
         *           bool Success = false;
         *           var Message = "Role Insert UnSuccessfull";
         *           return Conflict(new { Success, Message });
         *       }
         *   }
         *   catch (Exception exception)
         *   {
         *       bool Success = false;
         *       return BadRequest(new { Success, Message = exception.Message });
         *   }
         * }*/


        //Method to create JWT token
        private string CreateToken(RAdminLoginModel responseModel, string type)
        {
            try
            {
                var symmetricSecuritykey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"]));
                var signingCreds         = new SigningCredentials(symmetricSecuritykey, SecurityAlgorithms.HmacSha256);

                var claims = new List <Claim>();
                claims.Add(new Claim("RoleId", responseModel.RoleId.ToString()));
                claims.Add(new Claim("Role", responseModel.Role.ToString()));
                claims.Add(new Claim("Email", responseModel.EmailId.ToString()));
                claims.Add(new Claim("Id", responseModel.Id.ToString()));
                claims.Add(new Claim("TokenType", type));

                var token = new JwtSecurityToken(configuration["Jwt:Issuer"],
                                                 configuration["Jwt:Issuer"],
                                                 claims,
                                                 expires: DateTime.Now.AddHours(1),
                                                 signingCredentials: signingCreds);
                return(new JwtSecurityTokenHandler().WriteToken(token));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemple #2
0
        public async Task <IActionResult> AdminLogin([FromBody] AdminLoginModel adminLoginModel)
        {
            try
            {
                if (adminLoginModel.Password == null || adminLoginModel.Email == null)
                {
                    throw new Exception(AdminExceptions.ExceptionType.NULL_EXCEPTION.ToString());
                }

                //Throws Custom Exception When Fields are Empty Strings.
                if (adminLoginModel.Password == "" || adminLoginModel.Email == "")
                {
                    throw new Exception(AdminExceptions.ExceptionType.EMPTY_EXCEPTION.ToString());
                }

                RAdminLoginModel data = await this.adminBL.AdminLogin(adminLoginModel);

                if (data != null)
                {
                    data.Token = this.CreateToken(data, "authenticate role");
                    return(this.Ok(new { status = "True", message = "Login Successfully", data }));
                }
                else
                {
                    return(this.NotFound(new { status = "False", message = "Login UnSuccessfully" }));
                }
            }
            catch (Exception exception)
            {
                bool Success = false;
                return(BadRequest(new { Success, Message = exception.Message }));
            }
        }
        /// <summary>
        /// Admin login method
        /// </summary>
        /// <param name="adminLoginModel"></param>
        /// <returns></returns>
        public async Task <RAdminLoginModel> AdminLogin(AdminLoginModel adminLoginModel)
        {
            try
            {
                var user = await _userManger.FindByEmailAsync(adminLoginModel.Email);

                if (user == null)
                {
                    throw new Exception(AdminExceptions.ExceptionType.INVALID_EMAIL_IDENTITY.ToString());
                }
                var result = await _userManger.CheckPasswordAsync(user, adminLoginModel.Password);

                if (!result)
                {
                    throw new Exception(AdminExceptions.ExceptionType.INVALID_PASSWORD_IDENTITY.ToString());
                }

                adminLoginModel.Password = Encrypt(adminLoginModel.Password).ToString();
                DatabaseConnection databaseConnection = new DatabaseConnection(this.configuration);
                SqlConnection      sqlConnection      = databaseConnection.GetConnection();
                SqlCommand         sqlCommand         = databaseConnection.GetCommand("spAdminLogin", sqlConnection);
                sqlCommand.Parameters.AddWithValue("@EmailId", adminLoginModel.Email);
                sqlCommand.Parameters.AddWithValue("@Password", adminLoginModel.Password);
                sqlConnection.Open();
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

                var userData = new RAdminLoginModel();
                while (sqlDataReader.Read())
                {
                    int status = sqlDataReader.GetInt32(0);
                    if (status == 0)
                    {
                        return(null);
                    }
                    userData.Id          = (int)sqlDataReader["ID"];
                    userData.Name        = sqlDataReader["Name"].ToString();
                    userData.EmailId     = sqlDataReader["EmailId"].ToString();
                    userData.Role        = sqlDataReader["Role"].ToString();
                    userData.RoleId      = Convert.ToInt32(sqlDataReader["RoleId"]);
                    userData.Gender      = sqlDataReader["Gender"].ToString();
                    userData.CreatedDate = sqlDataReader["ModificateDate"].ToString();
                }

                if (userData != null)
                {
                    return(userData);
                }
                return(null);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Exemple #4
0
        public async Task <IActionResult> UpdateModel(UpdateModel updateModel)
        {
            try
            {
                RAdminLoginModel responseUser = null;

                if (!updateModel.Equals(null))
                {
                    //Throws Custom Exception When Fields are Empty Strings.
                    if (updateModel.EmailId == "" || updateModel.Gender == "" || updateModel.Name == "")
                    {
                        throw new Exception(AdminExceptions.ExceptionType.EMPTY_EXCEPTION.ToString());
                    }

                    //Throws Custom Exception When Fields are Empty Strings.
                    if (updateModel.EmailId == null || updateModel.Gender == null || updateModel.Name == null)
                    {
                        throw new Exception(AdminExceptions.ExceptionType.NULL_EXCEPTION.ToString());
                    }

                    string Role  = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Role").Value;
                    string Email = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "Email").Value;
                    //int RoleId = Convert.ToInt32(HttpContext.User.Claims.FirstOrDefault(c => c.Type == "RoleId").Value);

                    responseUser = await adminBL.UpdateModel(updateModel, Email);
                }
                else
                {
                    throw new Exception(AdminExceptions.ExceptionType.NULL_EXCEPTION.ToString());
                }

                if (!responseUser.Equals(null))
                {
                    bool Success = true;
                    var  Message = "Model Update Successfull";
                    return(Ok(new { Success, Message, }));
                }
                else
                {
                    bool Success = false;
                    var  Message = "Model Update UnSuccessfull";
                    return(Conflict(new { Success, Message }));
                }
            }
            catch (Exception exception)
            {
                bool Success = false;
                return(BadRequest(new { Success, Message = exception.Message }));
            }
        }
        public async Task <RAdminLoginModel> UpdateModel(UpdateModel updateModel, string Email)
        {
            try
            {
                var user = await _userManger.FindByEmailAsync(Email);

                if (user == null)
                {
                    throw new Exception(AdminExceptions.ExceptionType.INVALID_EMAIL_IDENTITY.ToString());
                }

                user.Email    = updateModel.EmailId;
                user.UserName = updateModel.Name;


                var result = await _userManger.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    throw new Exception(AdminExceptions.ExceptionType.INVALID_PASSWORD_IDENTITY.ToString());
                }

                RAdminLoginModel   rUser = new RAdminLoginModel();
                DatabaseConnection databaseConnection = new DatabaseConnection(this.configuration);
                SqlConnection      sqlConnection      = databaseConnection.GetConnection();
                SqlCommand         sqlCommand         = databaseConnection.GetCommand("spUpdateModel", sqlConnection);
                sqlCommand.Parameters.AddWithValue("@EmailId", updateModel.EmailId);
                sqlCommand.Parameters.AddWithValue("@Gender", updateModel.Gender);
                sqlCommand.Parameters.AddWithValue("@Name", updateModel.Name);
                //sqlCommand.Parameters.AddWithValue("@RoleId", RoleId);
                //sqlCommand.Parameters.AddWithValue("@Role", updateModel.Role);
                sqlCommand.Parameters.AddWithValue("@Email", Email);
                sqlConnection.Open();

                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    rUser.Id = sqlDataReader.GetInt32(0);
                    if (rUser.Id > 0)
                    {
                        rUser.CreatedDate = sqlDataReader["ModificateDate"].ToString();
                        rUser.Name        = sqlDataReader["Name"].ToString();
                        rUser.Gender      = sqlDataReader["Gender"].ToString();
                        rUser.Role        = sqlDataReader["Role"].ToString();
                        rUser.RoleId      = Convert.ToInt32(sqlDataReader["RoleId"]);
                        rUser.EmailId     = sqlDataReader["EmailId"].ToString();
                        return(rUser);
                    }
                    else
                    {
                        return(null);
                    }
                }

                return(null);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }