コード例 #1
0
ファイル: AccountController.cs プロジェクト: xiazhaowei/hmh
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            model.CheckNotNull(nameof(model));

            if (Session["Reg_EmailCode"] == null || Session["Reg_EmailCode"].ToString() != model.EmailCode)
            {
                return(Json(new AjaxResult("验证码错误", AjaxResultType.Error)));
            }

            UserInputDto dto = model.MapTo <UserInputDto>();

            dto.NickName       = dto.UserName;
            dto.RecommendId    = dto.RecommendId == 0 ? 1 : dto.RecommendId;
            dto.EmailConfirmed = true; //邮箱通过验证

            OperationResult result = await IdentityContract.CreateUsers(dto);

            if (result.ResultType == OperationResultType.Success)
            {
                //初始化用户角色
                User newuser = IdentityContract.Users.SingleOrDefault(u => u.UserName == dto.UserName);
                if (newuser != null)
                {
                    UserRoleMapInputDto mapDto = new UserRoleMapInputDto()
                    {
                        UserId = newuser.Id, RoleId = 2
                    };
                    result = await IdentityContract.CreateUserRoleMaps(mapDto);

                    if (!result.Successed)
                    {
                        return(Json(new AjaxResult(result.Message, AjaxResultType.Error)));
                    }
                }
                #region 用户登录
                LoginInfo loginInfo = new LoginInfo
                {
                    UserName = dto.UserName,
                    Password = dto.Password,
                    Remember = false
                };
                OperationResult <User> loginresult = await IdentityContract.Login(loginInfo, true);

                if (loginresult.ResultType == OperationResultType.Success)
                {
                    User user = loginresult.Data;
                    AuthenticationManager.SignOut();
                    await SignInManager.SignInAsync(user, loginInfo.Remember, true);
                }
                #endregion
                return(Json(new AjaxResult("登录成功", AjaxResultType.Success)));
            }
            else
            {
                return(Json(new AjaxResult(result.Message, AjaxResultType.Error)));
            }
        }