Ejemplo n.º 1
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult <RepositoryResponse <AccessTokenViewModel> > > Login([FromBody] LoginViewModel model)
        {
            RepositoryResponse <AccessTokenViewModel> loginResult = new RepositoryResponse <AccessTokenViewModel>();

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(
                    model.UserName, model.Password, isPersistent : model.RememberMe, lockoutOnFailure : true).ConfigureAwait(false);

                if (result.Succeeded)
                {
                    var user = await _userManager.FindByNameAsync(model.UserName).ConfigureAwait(false);

                    var token = await _helper.GenerateAccessTokenAsync(user, model.RememberMe);

                    if (token != null)
                    {
                        var info = await UserInfoViewModel.Repository.GetSingleModelAsync(u => u.Username == user.UserName);

                        if (!info.IsSucceed)
                        {
                            info.Data = new UserInfoViewModel();
                        }
                        token.UserData = info.Data;

                        loginResult.IsSucceed = true;
                        loginResult.Status    = 1;
                        loginResult.Data      = token;
                        _logger.LogInformation("User logged in.");
                        return(Ok(loginResult));
                    }
                    else
                    {
                        return(Ok(loginResult));
                    }
                }
                else
                {
                    loginResult.Errors.Add("login failed");
                    return(BadRequest(loginResult));
                }
            }
            else
            {
                return(BadRequest(loginResult));
            }
        }
Ejemplo n.º 2
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult <RepositoryResponse <AccessTokenViewModel> > > Login([FromBody] LoginViewModel model)
        {
            RepositoryResponse <AccessTokenViewModel> loginResult = new RepositoryResponse <AccessTokenViewModel>();

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(
                    model.UserName, model.Password, isPersistent : model.RememberMe, lockoutOnFailure : true).ConfigureAwait(false);

                if (result.Succeeded)
                {
                    var user = await _userManager.FindByNameAsync(model.UserName).ConfigureAwait(false);

                    var token = await _helper.GenerateAccessTokenAsync(user, model.RememberMe);

                    if (token != null)
                    {
                        token.Info = new MixUserViewModel(user);
                        await token.Info.LoadUserDataAsync();

                        loginResult.IsSucceed = true;
                        loginResult.Status    = 1;
                        loginResult.Data      = token;
                        _logger.LogInformation("User logged in.");
                        return(Ok(loginResult));
                    }
                    else
                    {
                        return(Ok(loginResult));
                    }
                }
                if (result.IsLockedOut)
                {
                    loginResult.Errors.Add("This account has been locked out, please try again later.");
                    return(BadRequest(loginResult));
                }
                else
                {
                    loginResult.Errors.Add("Login failed");
                    return(BadRequest(loginResult));
                }
            }
            else
            {
                return(BadRequest(loginResult));
            }
        }
Ejemplo n.º 3
0
        public async Task <RepositoryResponse <AccessTokenViewModel> > InitSuperAdmin([FromBody] MixRegisterViewModel model)
        {
            RepositoryResponse <AccessTokenViewModel> result = new RepositoryResponse <AccessTokenViewModel>();

            if (ModelState.IsValid)
            {
                if (_userManager.Users.Count() == 0)
                {
                    var user = new ApplicationUser
                    {
                        UserName  = model.Username,
                        Email     = model.Email,
                        FirstName = model.FirstName,
                        LastName  = model.LastName,
                        Avatar    = model.Avatar ?? MixService.GetConfig <string>("DefaultAvatar"),
                        JoinDate  = DateTime.UtcNow
                    };
                    var createResult = await _userManager.CreateAsync(user, password : model.Password).ConfigureAwait(false);

                    if (createResult.Succeeded)
                    {
                        user = await _userManager.FindByEmailAsync(model.Email).ConfigureAwait(false);

                        await _userManager.AddToRoleAsync(user, "SuperAdmin");

                        var token = await _idHelper.GenerateAccessTokenAsync(user, true);

                        if (token != null)
                        {
                            result.IsSucceed = true;
                            MixService.LoadFromDatabase();
                            MixService.SetConfig("InitStatus", 2);
                            MixService.SaveSettings();
                            MixService.Reload();
                            result.Data = token;
                            return(result);
                        }
                        else
                        {
                            return(result);
                        }
                    }
                    else
                    {
                        foreach (var error in createResult.Errors)
                        {
                            result.Errors.Add(error.Description);
                        }
                        return(result);
                    }
                }
            }

            return(result);
        }