// Fetch access token from Spotify public async Task <string> GetClientCredentialsAuthTokenAsync(string key) { string token = null; string Endpoint = "https://accounts.spotify.com/api/token"; var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "client_credentials") }); var authHeader = Convert.ToBase64String(Encoding.Default.GetBytes(key)); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Add authHeader without validation since it may look like more than one string client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic " + authHeader); HttpResponseMessage response = new HttpResponseMessage(); response = await client.PostAsync(Endpoint, content); if (response.IsSuccessStatusCode) { var httpContent = response.Content; StreamReader reader = new StreamReader(await httpContent.ReadAsStreamAsync(), Encoding.UTF8); AuthenticationResponseDTO auth = (AuthenticationResponseDTO)JsonConvert.DeserializeObject(reader.ReadToEnd(), typeof(AuthenticationResponseDTO)); token = auth.Access_Token; } return(token); }
public UserBusiness() { _userRepository = new UserRepository <UserResponseDTO, UserRequestDTO>(); _securityResponse = new SecurityRepository <AuthenticationResponseDTO, AuthenticationRequestDTO>(); _response = new UserResponseDTO(); _responseAuth = new AuthenticationResponseDTO(); }
public bool InsertToken(AuthenticationResponseDTO data) { var blnReturn = false; try { if (_securityRepository.InsertToken(data) == 1) { blnReturn = true; } } catch (Exception ex) { return(blnReturn); } return(blnReturn); }
public static HttpResponseMessage GetMessageUnauthorizedLogin(HttpResponseMessage respMessage) { var authorize = new AuthenticationResponseDTO { hasAccess = false, expires = null, message = "Unauthorized", token = null, statusCode = HttpStatusCode.Unauthorized }; var listAuthorize = new List <AuthenticationResponseDTO> { authorize }; respMessage.Content = new ObjectContent <AuthenticationResponseDTO[]>(listAuthorize.ToArray(), new JsonMediaTypeFormatter()); return(respMessage); }
public static HttpResponseMessage GetMessageCreateUser(HttpResponseMessage respMessage, AuthenticationResponseDTO response) { var authorize = new AuthenticationResponseDTO { hasAccess = response.hasAccess, email = response.email, userName = response.userName, guid = response.guid, token = response.token, nameSystem = response.nameSystem, message = response.message, statusCode = response.statusCode }; var listAuthorize = new List <AuthenticationResponseDTO> { authorize }; respMessage.Content = new ObjectContent <AuthenticationResponseDTO[]>(listAuthorize.ToArray(), new JsonMediaTypeFormatter()); return(respMessage); }
public async Task <IActionResult> Authenticate([FromBody] AuthenticateUserDTO model) { var user = await userManager.FindByNameAsync(model.Username); // check if user with username exists if (user == null) { throw new BadRequestException("Username or password are incorrect."); } if (await userManager.CheckPasswordAsync(user, model.Password)) { var token = await CreateTokenAsync(user); int userTypeId; if (await userManager.IsInRoleAsync(user, "administrator")) { userTypeId = (int)UserTypeEnum.Administrator; } else { userTypeId = (int)UserTypeEnum.Korisnik; } var result = new AuthenticationResponseDTO { Id = user.Id, Firstname = user.Firstname, Lastname = user.Lastname, Token = token.Token, UserTypeId = userTypeId }; return(Ok(result)); } throw new NotAuthorizedException("Username or password are incorrect."); }
public AuthenticationBusiness() { _securityRepository = new SecurityRepository <AuthenticationResponseDTO, AuthenticationRequestDTO>(); _response = new AuthenticationResponseDTO(); }