Esempio n. 1
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            FormsAuthentication.Initialize();
            Credentials credentials = new Credentials();
            credentials.EmailId = TextBox1.Text;
            credentials.Password = TextBox2.Text;
            try
            {
                HttpClient client = new HttpClient();
                var empResponse = client.Authenticate(credentials);

                if (empResponse.Status.StatusCode.Equals("200")==true)
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1,
                        empResponse.Employee.Email,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(30),
                        true,
                        empResponse.Employee.Title,
                        FormsAuthentication.FormsCookiePath);

                    string hash = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(
                       FormsAuthentication.FormsCookieName,
                       hash);
                    Response.Cookies.Add(cookie);
                    Session["Response"] = empResponse.Employee.ToSession();

                    Response.Redirect(FormsAuthentication.GetRedirectUrl(empResponse.Employee.Email, true));
                }
            }
            catch (Exception)
            {
                Label3.Text = " Username or Password is incorrect ";

            }
        }
        public ActionResult Login(LoginModel model ,string returnUrl)
        {
            Credentials credentials = new Credentials();

            credentials.EmailId=model.UserName;
            credentials.Password = model.Password;

               EmployeeResponse employeeResponse = credentials.Authenticate(credentials);
               if (employeeResponse.Status.StatusCode != "200")
               {
               ModelState.AddModelError("", "The user name or Password is invalid");
               return View(model);

               }
               else
               {
               CreateAuthenticationTicket(employeeResponse.Employee);
               if (string.Equals(employeeResponse.Employee.Title, "HR", StringComparison.OrdinalIgnoreCase))
                   returnUrl = "HR/Employee";
               return RedirectToLocal(returnUrl);

               }
        }