Exemple #1
0
 public LoginViewModel(UserModel userModel, IClientAuthorizationProvider authorizationProvider)
 {
     this.userModel = userModel;
     this.authorizationProvider = authorizationProvider;
     this.loginCommand = new RelayCommand(this.LoginMethod, param => this.loginCanExecute);
 }
Exemple #2
0
        public bool Login(UserModel userModel)
        {
            this.GetTokenDictionary(userModel);
            this.username = userModel.UserName;
            this.IsAuthorized = this.tokenDictionary.ContainsKey("access_token");
            if (!this.IsAuthorized)
            {
                this.username = null;
                this.AuthorizedClient = null;
            }
            else
            {
                this.AuthorizedClient = this.GetAuthorizedClient();
            }

            return this.IsAuthorized;
        }
Exemple #3
0
        public bool Register(UserModel userModel, bool login = true)
        {
            if (userModel.Password != userModel.ConfirmPassword)
            {
                return false;
            }

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.PostAsJsonAsync(this.appPath + "/api/Account/Register", userModel).Result;
            if (!response.IsSuccessStatusCode)
            {
                return false;
            }
            else if (login)
            {
                this.Login(userModel);
            }

            return true;
        }
Exemple #4
0
        private void GetTokenDictionary(UserModel userModel)
        {
            var pairs = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("grant_type", "password"),
                new KeyValuePair<string, string>("username", userModel.UserName),
                new KeyValuePair<string, string>("password", userModel.Password)
            };
            FormUrlEncodedContent content = new FormUrlEncodedContent(pairs);

            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = client.PostAsync(this.appPath + "/token", content).Result;
                // TODO check whether authorization was successful
                string result = response.Content.ReadAsStringAsync().Result;
                this.tokenDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
            }
        }
        public RegisterViewModel(UserModel userModel, IClientAuthorizationProvider authorizationProvider)
        {
            this.userModel = userModel;
            this.authorizationProvider = authorizationProvider;
            this.registerCommand = new RelayCommand(this.RegisterMethod, param => this.registerCanExecute);

            Regex usernameAllowedSymbolsRegex = new Regex("^[a-z0-9_]*$", RegexOptions.Compiled);
            Regex usernameStartsWithRegex = new Regex("^[a-z]", RegexOptions.Compiled);
            Regex passwordTwoUppercaseLettersRegex = new Regex("^.*([A-Z].*){2,}$", RegexOptions.Compiled);
            Regex passwordTwoLowercaseLettersRegex = new Regex("^.*([a-z].*){2,}$", RegexOptions.Compiled);
            Regex passwordTwoCyphersRegex = new Regex("^.*([0-9].*){2,}$", RegexOptions.Compiled);

            this.AddValidationProperties(new List<ValidationProperty>
                {
                    new ValidationProperty
                    {
                        PropertyName = "UserName",
                        RuleSet = new List<ValidationRule>
                        {
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return this.UserName != null &&
                                        this.UserName.Length > 2 &&
                                        this.UserName.Length < 16;
                                },
                                ErrorMessage = "The username length must be 3-15 characters long."
                            },
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return string.IsNullOrEmpty(this.UserName) ||
                                        usernameAllowedSymbolsRegex.IsMatch(this.UserName);
                                },
                                ErrorMessage = "Allowed characters for the username are lowercase letters, cyphers, and the underscore symbol."
                            },
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return string.IsNullOrEmpty(this.UserName) ||
                                        usernameStartsWithRegex.IsMatch(this.UserName);
                                },
                                ErrorMessage = "The username have to start with lowercase letter."
                            },
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return !string.IsNullOrEmpty(this.UserName) &&
                                        this.UserName.Length > 2 &&
                                        this.authorizationProvider.IsUsernameAvailable(this.UserName);
                                },
                                ErrorMessage = "This username is already used."
                            }
                        }
                    },
                    new ValidationProperty
                    {
                        PropertyName = "Password",
                        RuleSet = new List<ValidationRule>
                        {
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return this.Password != null &&
                                        this.Password.Length > 7;
                                },
                                ErrorMessage = "The password must be at least 8 characters long."
                            },
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return string.IsNullOrEmpty(this.Password) ||
                                        passwordTwoUppercaseLettersRegex.IsMatch(this.Password);
                                },
                                ErrorMessage = "The password must have at least two uppercase letters."
                            },
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return string.IsNullOrEmpty(this.Password) ||
                                        passwordTwoLowercaseLettersRegex.IsMatch(this.Password);
                                },
                                ErrorMessage = "The password must have at least two lowercase letters."
                            },
                            new ValidationRule
                            {
                                Rule = () =>
                                {
                                    return string.IsNullOrEmpty(this.Password) ||
                                        passwordTwoCyphersRegex.IsMatch(this.Password);
                                },
                                ErrorMessage = "The password must have at least two cyphers."
                            }
                        }
                    },
                    new ValidationProperty
                    {
                        PropertyName = "ConfirmPassword",
                        RuleSet = new List<ValidationRule>
                        {
                            new ValidationRule
                            {
                                Rule = () => !string.IsNullOrEmpty(this.ConfirmPassword),
                                ErrorMessage = string.Empty
                            },
                            new ValidationRule
                            {
                                Rule = () => this.Password == this.ConfirmPassword,
                                ErrorMessage = "Passwords does not match."
                            }
                        }
                    }
                });
            this.AddPropertySubscriber("Password", b => this.PasswordHasErrors = b);
            this.AddPropertySubscriber("ConfirmPassword", b => this.ConfirmPasswordHasErrors = b);
        }