Пример #1
0
        public UserLoginPostBackcode Post(UserRegisterPostParam param)
        {
            UserLoginPostBackcode backcode = new UserLoginPostBackcode();

            if (context.Users.Any(c => c.LoginName == param.LoginName))
            {
                backcode.Code = 201;
                backcode.Msg  = "当前账户名已存在";
            }
            else
            {
                DB.User user = new DB.User();
                user.IdCard    = param.IdCard;
                user.LoginName = param.LoginName;
                user.LoginPwd  = param.LoginPwd;
                user.RealName  = param.RealName;
                user.WorkCard  = param.WorkCard;
                user.Enable    = true;

                context.Users.Add(user);
                context.SaveChanges();

                HttpContext.Session.SetObjectAsJson(SessionNames.LoginUser, user);
            }

            return(backcode);
        }
Пример #2
0
        public UserLoginPostBackcode Post(UserLoginPostParam _param)
        {
            var _backcode = new UserLoginPostBackcode();

            var _userModel = context.Users.SingleOrDefault(c => c.LoginName == _param.UserName && c.LoginPwd == _param.Password && c.Enable);

            if (_userModel == null || _userModel.Id <= 0)
            {
                _backcode.Code = 201;
                _backcode.Msg  = "用户名或密码错误";
            }
            else
            {
                #region 创建Cookie

                ////1.创建cookie 保存用户信息,使用claim。将序列化用户信息并将其存储在cookie中
                //var claims = new List<Claim>()
                //{
                //    new Claim(ClaimTypes.Name,_userModel.LoginName),
                //    new Claim("Id",_userModel.Id.ToString())
                //};

                ////2.创建声明主题 指定认证方式 这里使用cookie
                //var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                ////3.配置认证属性 比如过期时间,是否持久化。。。。
                //var authProperties = new AuthenticationProperties
                //{
                //AllowRefresh = <bool>,
                // Refreshing the authentication session should be allowed.

                //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                // The time at which the authentication ticket expires. A
                // value set here overrides the ExpireTimeSpan option of
                // CookieAuthenticationOptions set with AddCookie.

                //IsPersistent = true,
                //持久化 ,比如 登录的时候 勾选记住我 复选框

                //IssuedUtc = <DateTimeOffset>,
                //绝对cookie过期

                //RedirectUri = <string>
                // The full path or absolute URI to be used as an http
                // redirect response value.
                //};

                //4.登录
                //await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

                #endregion

                HttpContext.Session.SetObjectAsJson(SessionNames.LoginUser, _userModel);
            }

            return(_backcode);
        }