Пример #1
0
        public async Task <IActionResult> GetToken([FromBody] AuthBindingModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await UserMgr.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    var signInResult = await SignInMgr.CheckPasswordSignInAsync(user, model.Password.ToString(), false);

                    if (signInResult.Succeeded)
                    {
                        var roles = await UserMgr.GetRolesAsync(user);

                        return(generateToken(user, roles.FirstOrDefault()));
                    }
                    else
                    {
                        var err2 = new { status = "error", message = "Authentication Failed ! Check Email & Password" };
                        return(BadRequest(err2));
                    }
                }

                var err = new { status = "error", message = "Could not find a user for given Email!" };
                return(BadRequest(err));
            }

            return(BadRequest());
        }
Пример #2
0
        private void Login()
        {
            var model = new AuthBindingModel();

            //+ поля
            model.Login    = LoginTextBox.Text;
            model.Password = PasswordTextBox.Text;

            var validation = _validationService.Validation(model);

            if (!validation.Success)
            {
                MessageBox.Show(validation.Error.ErrorDescription);
                return;
            }

            var result = _accountService.Login(model);

            if (!result.Success)
            {
                MessageBox.Show(result.Error.ErrorDescription);
                return;
            }
            if (result.Success)
            {
                this.Hide();
                var homePage = new HomePage(result.Result);
                homePage.Show();
            }
        }
Пример #3
0
        // true якщо поля пусті
        bool IsFilled(AuthBindingModel model)
        {
            bool isFilled = false;

            isFilled = model.Login == "" || model.Password == "";
            //string.IsNullOrWhiteSpace(model.Login);
            return(isFilled);
        }
Пример #4
0
        public async Task <IActionResult> GenerateToken([FromBody] AuthBindingModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await UserMgr.FindByEmailAsync(model.Email);

                if (user != null)
                {
                    var signInResult = await SignInMgr.CheckPasswordSignInAsync(user, model.Password.ToString(), false);

                    if (signInResult.Succeeded)
                    {
                        var key  = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JWTSettings.Secret));
                        var cred = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                        var claims = new[]
                        {
                            new Claim(JwtRegisteredClaimNames.Sub, user.Email),
                            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                            new Claim("UserType", ""),
                        };

                        var token = new JwtSecurityToken(

                            JWTSettings.Issuer,
                            JWTSettings.Audience,
                            claims,
                            expires: DateTime.UtcNow.AddMinutes(JWTSettings.ExpieryTimeInMins),
                            signingCredentials: cred

                            );

                        var results = new
                        {
                            thisuser   = new { UserName = user.UserName, Email = user.Email },
                            token      = new JwtSecurityTokenHandler().WriteToken(token),
                            expiration = token.ValidTo
                        };
                        return(Ok(results));
                    }
                    else
                    {
                        var err2 = new { status = "error", message = "Authentication Failed ! Check UserName & Password" };
                        return(BadRequest(err2));
                    }
                }

                var err = new { status = "error", message = "Could not find a user!" };
                return(BadRequest(err));
            }

            return(BadRequest());
        }
Пример #5
0
        public ServiceResult <ApplicationUser> Validation(AuthBindingModel model)
        {
            var serviceResult = new ServiceResult <ApplicationUser>();

            // чи заповнені всі поля
            if (IsFilled(model))
            {
                serviceResult.Error.ErrorCode        = 400;
                serviceResult.Error.ErrorDescription = "Not all fields are filled";
                return(serviceResult);
            }
            serviceResult.Success = true;
            return(serviceResult);
        }
Пример #6
0
        bool PasswordIsСorrect(AuthBindingModel model)
        {
            bool            passIsCorr = false;
            string          login      = "******"" + model.Login + "\"";
            string          query      = "SELECT * FROM `Users` WHERE Login = "******"Password"] + "" == model.Password)
            {
                passIsCorr = true;
            }
            dataReader.Close();

            return(passIsCorr);
        }
Пример #7
0
        public ServiceResult <ApplicationUser> Login(AuthBindingModel model)
        {
            var serviceResult = new ServiceResult <ApplicationUser>();

            //check

            try
            {
                //SetConn();
                //conn.Open();

                if (LoginExist(model.Login))
                {
                    if (PasswordIsСorrect(model))
                    {
                        var instanse = Singleton.getInstance();
                        instanse.User         = GetUser(model);
                        serviceResult.Result  = instanse.User;
                        serviceResult.Success = true;
                    }
                    else
                    {
                        serviceResult.Error.ErrorCode        = 405;
                        serviceResult.Error.ErrorDescription = "Wrong password";
                    }
                }
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                serviceResult.Error.ErrorCode        = 100;
                serviceResult.Error.ErrorDescription = ex.Message;
                serviceResult.Success = false;
            }

            //conn.Close();

            return(serviceResult);
        }
Пример #8
0
        ApplicationUser GetUser(AuthBindingModel model)
        {
            var user = new ApplicationUser();

            string login = "******"" + model.Login + "\"";
            string query = "SELECT * FROM `Users` WHERE Login = "******"Id"] + "";
            user.FirstName   = dataReader["FirstName"] + "";
            user.LastName    = dataReader["LastName"] + "";
            user.Login       = dataReader["Login"] + "";
            user.Password    = dataReader["Password"] + "";
            user.PhoneNumber = dataReader["PhoneNumber"] + "";
            user.Email       = dataReader["Email"] + "";

            dataReader.Close();

            return(user);
        }