コード例 #1
0
        private void login_Click(object sender, RoutedEventArgs e)
        {
            if (!btn_login.IsEnabled)
            {
                return;
            }
            btn_login.IsEnabled = false;
            string name = txtUserName.GetTextBoxText();
            string pass = txtPassword.Password;

            if (name.IsNullOrEmpty() || pass.IsNullOrEmpty())
            {
                "用户名和密码不能为空".MessageBoxDialog();
                btn_login.IsEnabled = true;
                return;
            }

            AspNetUser user = new AspNetUser
            {
                UserName     = name,
                PasswordHash = pass
            };

            try
            {
                info.IsAdmitted(user, ((o, args) =>
                {
                    if (args.Error != null)
                    {
                        args.Error.Message.MessageBoxDialog();
                        btn_login.IsEnabled = true;
                        return;
                    }

                    WebClient client = o as WebClient;
                    var result = JsonConvert.DeserializeObject <Dictionary <string, string> >(Encoding.UTF8.GetString(args.Result));
                    string msg = result["Errors"];
                    if (!msg.IsNullOrEmpty())
                    {
                        msg.MessageBoxDialog();
                    }
                    else
                    {
                        string r = result["Succeeded"];
                        if (null != r && "true" == r)
                        {
                            MainWindow main = new MainWindow();
                            Application.Current.MainWindow = main;

                            Application.Current.MainWindow.Tag = new LoginedUserInfo
                            {
                                RoleName = result["Role"],
                                Permitted = result["Role"] != "Member",//如为true则有权限进行编辑
                                UGuid = result["UGuid"],
                                UName = name
                            };
                            main.Show();
                            this.Close();
                        }
                        else
                        {
                            "密码错误".MessageBoxDialog();
                        }
                    }
                    client.Dispose();
                    btn_login.IsEnabled = true;
                }));
            }
            catch (Exception ex)
            {
                ex.Message.MessageBoxDialog();
            }
        }