Пример #1
0
        public async Task <ActionResult <RepositoryResponse <AccessTokenViewModel> > > Register([FromBody] MixRegisterViewModel model)
        {
            RepositoryResponse <AccessTokenViewModel> result = new RepositoryResponse <AccessTokenViewModel>();

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName  = model.Username,
                    Email     = model.Email,
                    FirstName = model.FirstName,
                    LastName  = model.LastName,
                    Avatar    = model.Avatar ?? MixService.GetAppSetting <string>("DefaultAvatar"),
                    JoinDate  = DateTime.UtcNow
                };

                var createResult = await _userManager.CreateAsync(user, password : model.Password).ConfigureAwait(false);

                if (createResult.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user, MixDefaultRoles.Guest);

                    var saveData = await Mix.Cms.Lib.ViewModels.MixDatabaseDatas.Helper.SaveObjAsync(
                        MixDatabaseNames.SYSTEM_USER_DATA, model.UserData, user.UserName, MixDatabaseParentType.User);

                    result.IsSucceed = saveData.IsSucceed;
                    result.Errors    = saveData.Errors;
                    result.Exception = saveData.Exception;

                    _logger.LogInformation("User created a new account with password.");
                    user = await _userManager.FindByNameAsync(model.Username).ConfigureAwait(false);

                    var rsaKeys = RSAEncryptionHelper.GenerateKeys();
                    var aesKey  = AesEncryptionHelper.GenerateCombinedKeys(256);
                    var token   = await _idService.GenerateAccessTokenAsync(user, true, aesKey, rsaKeys[MixConstants.CONST_RSA_PUBLIC_KEY]);

                    if (token != null)
                    {
                        result.IsSucceed = true;
                        result.Data      = token;
                        _logger.LogInformation("User logged in.");
                        return(result);
                    }
                    else
                    {
                        return(Ok(result));
                    }
                }
                else
                {
                    foreach (var error in createResult.Errors)
                    {
                        result.Errors.Add(error.Description);
                    }
                    return(BadRequest(result));
                }
            }

            return(BadRequest(result));
        }
Пример #2
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, MixRoles.SuperAdmin);

                        await MixAccountHelper.LoadUserInfoAsync(user.UserName);

                        var rsaKeys = RSAEncryptionHelper.GenerateKeys();
                        var aesKey  = MixService.GetConfig <string>(MixAppSettingKeywords.ApiEncryptKey);

                        var token = await _idHelper.GenerateAccessTokenAsync(user, true, aesKey, rsaKeys[MixConstants.CONST_RSA_PUBLIC_KEY]);

                        if (token != null)
                        {
                            result.IsSucceed = true;
                            MixService.LoadFromDatabase();
                            MixService.SetConfig <string>(MixAppSettingKeywords.ApiEncryptKey, aesKey);
                            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);
        }