public ActionResult Login(AuthModels.Login model) { if (ModelState.IsValid) { string token = null; try { token = HarmonyLogin.GetUserAuthToken(model.Username, model.Password); } catch { } if (string.IsNullOrEmpty(token)) { ModelState.AddModelError("Username", "Invalid username or password"); } if (ModelState.IsValid) { using (var manager = new Manager()) { var user = manager.Users .FirstOrDefault(o => o.HarmonyUsername == model.Username); if (user == null) { user = new User() { HarmonyUsername = model.Username }; } user.HarmonyPassword = ""; user.HarmonyToken = token; user.AlexaToken = RandomString(50); user.AlexaUserID = ""; user.Hostname = model.Hostname; manager.SaveChanges(); string url = ConfigurationManager.AppSettings["AuthUrl"] + "#state=" + Url.Encode(model.State) + "&access_token=" + Url.Encode(user.AlexaToken) + "&token_type=Bearer"; return Redirect(url); } } } return View(model); }
public ActionResult Index(AuthModels.Index model) { if (ModelState.IsValid) { var model2 = new AuthModels.Network { State = model.State, Hostname = Request.UserHostAddress }; return View("Network", model2); } return View(model); }
public void SuccessAuthAfterRegistrationTest() { AccountRegistrationModel newUser = new AccountRegistrationModel(); var response = lykkeApi.Registration.PostRegistrationResponse(newUser); Assert.That(response.Error, Is.Null, $"Error message not empty {response.Error?.Message}"); AuthenticateModel auth = new AuthenticateModel(newUser); var authResponse = lykkeApi.Auth.PostAuthResponse(auth); Assert.That(authResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK), "Invalid status code"); var obj = JObject.Parse(authResponse.Content); Assert.That(obj.IsValid(apiSchemes.AuthScheme.AuthResponseScheme), Is.True, "Responce JSON is not valid scheme"); var authModel = AuthModels.ConvertToAuthModelResponse(authResponse.Content); Assert.That(authModel.Result.PersonalData.FullName, Is.EqualTo(newUser.FullName), "Invalid Full Name"); }
public async static Task <string> GetAccessToken() { if (tokenResponse != null) { return(tokenResponse.access_token); } var values = new Dictionary <string, string> { { "client_id", clientId }, { "scope", scope }, { "client_secret", clientSecret }, { "grant_type", grantType } }; var content = new FormUrlEncodedContent(values); var response = await client.PostAsync(tokenUri, content); var responseString = await response.Content.ReadAsStringAsync(); tokenResponse = JsonHelper.Deserialize <AuthModels>(responseString); return(tokenResponse.access_token); }
public string AutenticarUsuario(string usuario, string senha) { AuthModels auth = new AuthModels(); if (usuario.ToUpper().Equals("ADMIN") && senha.Equals("helper2019")) { auth.Autenticado = true; auth.Id = 0; auth.Perfil = 1; auth.Usuario = "ADMIN"; } else { BancoDados b = new BancoDados(); b.Query(@"SELECT * FROM USUARIOS where USU_USUARIO = ?USU_USUARIO and USU_SENHA = ?USU_SENHA"); b.SetParametro("?USU_USUARIO", usuario); b.SetParametro("?USU_SENHA", FormsAuthentication.HashPasswordForStoringInConfigFile(senha, "md5")); DataTable dt = b.ExecutarDataTable(); if (dt.Rows.Count > 0) { auth.Autenticado = true; auth.Id = int.Parse(dt.Rows[0]["USU_CODIGOID"].ToString().ToUpper()); auth.Perfil = int.Parse(dt.Rows[0]["USU_PERFIL"].ToString().ToUpper()); auth.Usuario = dt.Rows[0]["USU_USUARIO"].ToString().ToUpper(); } else { auth.Autenticado = false; } } return(JsonConvert.SerializeObject(auth)); }
public ActionResult Network(AuthModels.Network model) { if (ModelState.IsValid) { bool success = false; try { var conn = new HarmonyClientConnection(model.Hostname, 5222); var manualEvent = new ManualResetEvent(false); conn.ClientSocket.OnConnect += (obj) => { success = true; manualEvent.Set(); }; conn.OnSocketError += (obj, ex) => { manualEvent.Set(); }; try { conn.Open(); manualEvent.WaitOne(5000); } finally { try { conn.Close(); } catch { } } } catch { } if (!success) { ModelState.AddModelError("Hostname", "Your Harmony Hub could not be reached"); } if (ModelState.IsValid) { var model2 = new AuthModels.Login { State = model.State, Hostname = Request.UserHostAddress }; return View("Login", model2); } } return View(model); }