/// <summary> /// Generates Token /// </summary> /// <param name="adminDetails">Admin Response Details</param> /// <param name="tokenType">Token Type</param> /// <returns>It return token else exception</returns> private string GenerateToken(AdminRegistrationResponse adminDetails, string tokenType) { try { var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"])); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); var claims = new[] { new Claim("AdminID", adminDetails.AdminID.ToString()), new Claim("Email", adminDetails.Email.ToString()), new Claim("TokenType", tokenType), new Claim("UserRole", adminDetails.UserRole.ToString()) }; var token = new JwtSecurityToken(_configuration["Jwt:Issuer"], _configuration["Jwt:Issuer"], claims, expires: DateTime.Now.AddDays(1), signingCredentials: credentials); return(new JwtSecurityTokenHandler().WriteToken(token)); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// Add admin details into the database /// </summary> /// <param name="adminDetails">Admin Registration Details</param> /// <returns>If data added successully, return response data else null or exception</returns> public async Task <AdminRegistrationResponse> AdminRegistration(AdminRegistrationRequest adminDetails) { try { AdminRegistrationResponse responseData = null; SQLConnection(); using (SqlCommand cmd = new SqlCommand("AddAdminDetails", conn)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@FirstName", adminDetails.FirstName); cmd.Parameters.AddWithValue("@LastName", adminDetails.LastName); cmd.Parameters.AddWithValue("@Email", adminDetails.Email); cmd.Parameters.AddWithValue("@Password", adminDetails.Password); cmd.Parameters.AddWithValue("@IsActive", true); cmd.Parameters.AddWithValue("@UserRole", _admin); cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now); cmd.Parameters.AddWithValue("@ModifiedDate", DateTime.Now); conn.Open(); SqlDataReader dataReader = await cmd.ExecuteReaderAsync(); responseData = AdminRegistrationResponseModel(dataReader); }; return(responseData); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// Verify Admin Login Details /// </summary> /// <param name="loginDetails">Admin Login Details</param> /// <returns>If data verified, return response data else ull or exception</returns> public async Task <AdminRegistrationResponse> AdminLogin(AdminLoginRequest loginDetails) { try { AdminRegistrationResponse responseData = null; SQLConnection(); using (SqlCommand cmd = new SqlCommand("ValidateAdminLogin", conn)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Email", loginDetails.Email); cmd.Parameters.AddWithValue("@Password", loginDetails.Password); cmd.Parameters.AddWithValue("@UserRole", _admin); conn.Open(); SqlDataReader dataReader = await cmd.ExecuteReaderAsync(); responseData = AdminRegistrationResponseModel(dataReader); }; return(responseData); } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// Admin Registration Response Method /// </summary> /// <param name="dataReader">Sql Data Reader</param> /// <returns>It return Response Data or Exception</returns> private AdminRegistrationResponse AdminRegistrationResponseModel(SqlDataReader dataReader) { try { AdminRegistrationResponse responseData = null; while (dataReader.Read()) { responseData = new AdminRegistrationResponse() { AdminID = Convert.ToInt32(dataReader["AdminID"]), FirstName = dataReader["FirstName"].ToString(), LastName = dataReader["LastName"].ToString(), Email = dataReader["Email"].ToString(), IsActive = Convert.ToBoolean(dataReader["IsActive"]), UserRole = dataReader["UserRole"].ToString(), CreatedDate = Convert.ToDateTime(dataReader["CreatedDate"]), ModifiedDate = Convert.ToDateTime(dataReader["ModifiedDate"]) }; } return(responseData); } catch (Exception ex) { throw new Exception(ex.Message); } }