public Boolean Register(String login, String password) { var authData = new CAuthorizationData(login, password); HttpResponseMessage response = SHelper.PostRaw("api/Authorization/Registration", authData); return(response.IsSuccessStatusCode); }
public IActionResult Login(CAuthorizationData data) { CUser user = Authenticator.Find(data); if (user != null) { HttpContext.Session.SetString("user", JsonConvert.SerializeObject(data)); return(RedirectToRoute(new { controller = "Home", action = "Index" })); } return(RedirectToRoute(new { controller = "Home", action = "Login" })); }
public Boolean TryAuthorize(String login, String password, out CAuthToken token) { var authData = new CAuthorizationData(login, password); HttpResponseMessage response = SHelper.PostRaw("api/Authorization/Login", authData); if (response.IsSuccessStatusCode && response.StatusCode == HttpStatusCode.OK) { String result = response.Content.ReadAsStringAsync().Result; token = JsonConvert.DeserializeObject <CAuthToken>(result); return(true); } token = null; return(false); }
private Boolean ValidateAuthModel(CAuthorizationData model, out String error) { error = String.Empty; if (String.IsNullOrWhiteSpace(model.Login)) { error = "Login is required field"; return(false); } if (String.IsNullOrWhiteSpace(model.Password)) { error = "Password is required field"; return(false); } return(true); }
public IHttpActionResult RegisterUser([FromBody] CAuthorizationData model) { if (!ValidateAuthModel(model, out String errorMessage)) { return(BadRequest(errorMessage)); } using (var client = new HttpClient()) { String authServiceUrl = GetAuthServiceUrl(); var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8); HttpResponseMessage response = client.PostAsync(authServiceUrl + "api/Authorization/Registration", content).Result; if (response.IsSuccessStatusCode) { return(Ok()); } return(BadRequest()); } }
public IHttpActionResult LoginUser([FromBody] CAuthorizationData model) { if (String.IsNullOrWhiteSpace(model.Login)) { return(BadRequest("Login is required field")); } if (String.IsNullOrWhiteSpace(model.Password)) { return(BadRequest("Password is required field")); } var authProvider = new CAuthProvider(); Boolean authorizationResult = authProvider.TryAuthorize(model.Login, model.Password, out CAuthToken token); if (!authorizationResult) { return(BadRequest("Incorrect login or password")); } return(Ok(token)); }
public IHttpActionResult RegisterUser([FromBody] CAuthorizationData model) { if (String.IsNullOrWhiteSpace(model.Login)) { return(BadRequest("Login is required field")); } if (String.IsNullOrWhiteSpace(model.Password)) { return(BadRequest("Password is required field")); } var authProvider = new CAuthProvider(); Boolean userExist = authProvider.CheckLoginExist(model.Login); if (userExist) { return(BadRequest($"Login {model.Login} already exist")); } authProvider.RegisterUser(model.Login, model.Password); return(Ok()); }
public IHttpActionResult LoginUser([FromBody] CAuthorizationData model) { if (!ValidateAuthModel(model, out String errorMessage)) { return(BadRequest(errorMessage)); } using (var client = new HttpClient()) { String authServiceUrl = GetAuthServiceUrl(); //var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8); var content = new ObjectContent <CAuthorizationData>(model, new JsonMediaTypeFormatter()); HttpResponseMessage response = client.PostAsync(authServiceUrl + "api/Authorization/Login", content).Result; if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsStringAsync().Result; var token = JsonConvert.DeserializeObject <CAuthToken>(result); return(Ok(token)); } return(BadRequest()); } }