Exemplo n.º 1
0
        public async Task <ActionResult> Login([FromBody] BKLoginInfo loginInfo)
        {
            if (HttpContext.Session.GetString("user") != null)
            {
                return(Ok(CommonResult.Fail("already login")));
            }
            BkUser user = await _userReposity.GetUserByAccount(loginInfo.Account);

            if (user == null)
            {
                return(Ok(CommonResult.Fail("e - mail address don't exist")));
            }
            if (await _userInfoReposity.GetState(user.UId) == 0)
            {
                return(Ok(CommonResult.Fail("you have been banned, please contact administrator first")));
            }
            if (user.Password == MD5Util.GenerateMD5(loginInfo.Password, user.Salt))
            {
                HttpContext.Session.SetString("user", user.Account);
                return(Ok(CommonResult.Success(user.Account)));
            }
            else
            {
                return(Ok(CommonResult.Fail("password error")));
            }
        }
        public async Task <JsonResult> CreateAgent(AgentCredentialsViewModel model)
        {
            return(await RunWithResult(async() => {
                ValidateAgentCredentials(model);

                var userManager = HttpContext.GetOwinContext().GetUserManager <BkUserManager>();
                var roleManager = HttpContext.GetOwinContext().GetUserManager <BkRoleManager>();

                var existingUser = await userManager.FindByNameAsync(model.Code);
                if (existingUser != null)
                {
                    throw new PublicException($"Пользователь с именем {model.Code} уже существует");
                }

                if (!roleManager.RoleExists(UserRoleNames.Agent))
                {
                    var roleCreationResult = await roleManager.CreateAsync(new BkRole(UserRoleNames.Agent));
                    if (!roleCreationResult.Succeeded)
                    {
                        throw new PublicException(string.Join(". ", roleCreationResult.Errors));
                    }
                }

                var user = new BkUser {
                    UserName = model.Code
                };
                var userCreationResult = await userManager.CreateAsync(user, model.Password);
                if (!userCreationResult.Succeeded)
                {
                    throw new PublicException(string.Join(". ", userCreationResult.Errors));
                }

                var addToRoleResult = await userManager.AddToRoleAsync(user.Id, UserRoleNames.Agent);
                if (!addToRoleResult.Succeeded)
                {
                    throw new PublicException(string.Join(". ", addToRoleResult.Errors));
                }

                var agentId = await _agentRepository.CreateAgentAync(model.Code, model.Password);

                return new Agent {
                    Id = agentId,
                    Code = model.Code,
                    Password = model.Password
                };
            }, "При создании агента произошла ошибка"));
        }
Exemplo n.º 3
0
        public async Task <JsonResult> Register(string username, string password, string roleName)//TODO: remove
        {
            var r = (await RunWithResult(async() =>
            {
                var userManager = HttpContext.GetOwinContext().GetUserManager <BkUserManager>();
                var roleManager = HttpContext.GetOwinContext().GetUserManager <BkRoleManager>();

                var existingUser = await userManager.FindByNameAsync(username);
                if (existingUser != null)
                {
                    throw new PublicException($"Пользователь с именем {username} уже существует");
                }

                if (!roleManager.RoleExists(roleName))
                {
                    var roleCreationResult = await roleManager.CreateAsync(new BkRole(roleName));
                    if (!roleCreationResult.Succeeded)
                    {
                        throw new PublicException(string.Join(". ", roleCreationResult.Errors));
                    }
                }

                var user = new BkUser {
                    UserName = username
                };
                var userCreationResult = await userManager.CreateAsync(user, password);
                if (!userCreationResult.Succeeded)
                {
                    throw new PublicException(string.Join(". ", userCreationResult.Errors));
                }

                var addToRoleResult = await userManager.AddToRoleAsync(user.Id, roleName);
                if (!addToRoleResult.Succeeded)
                {
                    throw new PublicException(string.Join(". ", addToRoleResult.Errors));
                }

                return(user.Id);
            }, "При создании пользователя произошла ошибка"));

            r.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            return(r);
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Register([FromBody] BKRegisterInfo info)
        {
            int count = await _userReposity.CheckUserByAccountAndNickNameAsync(info.Account, info.NickName);

            if (count > 0)
            {
                // 先这样写
                return(Ok(CommonResult.Fail("account or nickName have been used")));
            }
            string salt = "baike";
            BkUser user = new BkUser {
                Account = info.Account, Password = MD5Util.GenerateMD5(info.Password, salt), Salt = salt
            };
            BkUserInfo userInfo = new BkUserInfo {
                NickName = info.NickName, State = 1, Icon = "user_default.jpg", BackgroundIcon = "back_default.jpg"
            };

            user.BkUserInfo = userInfo;
            _userReposity.AddUser(user);
            await _userReposity.SaveAsync();

            return(Ok(CommonResult.Success("register success")));
        }
Exemplo n.º 5
0
 public void AddUser(BkUser user)
 {
     _context.BkUser.Add(user);
 }