コード例 #1
0
        private void submit_login(object sender, RoutedEventArgs e)
        {
            var    client   = new AuthServiceClient();
            string email    = txt_email.Text;
            string password = txt_password.Password.ToString();

            try
            {
                string token = client.LoginUser(email, password);

                if (!string.IsNullOrEmpty(token))
                {
                    Application.Current.Resources["Token"] = token;

                    //create main window and dispose current one
                    MainWindow main = new MainWindow();
                    main.Show();
                    this.Close();
                }
                else
                {
                    lbl_error.Content = "Invalid email or password";
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                client.Close();
            }
        }
コード例 #2
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var result = AuthServiceClient.LoginUser(model.Email, model.Password);

            if (result)
            {
                var username = AuthServiceClient.GetUsernameFromEmail(model.Email);

                var identity = new ClaimsIdentity(
                    new[] { new Claim(ClaimTypes.Name, username), },
                    DefaultAuthenticationTypes.ApplicationCookie,
                    ClaimTypes.Name, ClaimTypes.Role);

                Authentication.SignIn(new AuthenticationProperties {
                    IsPersistent = false
                }, identity);

                return(RedirectToAction("Index", "Home"));
            }

            ModelState.AddModelError("", "Invalid login attempt.");
            return(View(model));
        }