コード例 #1
0
        //[ValidateAntiForgeryToken]
        public async Task <JsonResult> LoginModal(LoginViewModel model, string Code)
        {
            Auth auth = new Auth();

            if (!ModelState.IsValid)
            {
                auth.text = "用户名或密码错误";
                return(Json(auth, JsonRequestBehavior.AllowGet));
            }
            if (Code != null)
            {
                if (SessionHelper.GetSession("verifycode") == null || Code.ToLower() != SessionHelper.GetSession("verifycode").ToString())
                {
                    auth.text = "验证码错误";
                    return(Json(auth, JsonRequestBehavior.AllowGet));
                }
            }
            var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
            {
                String code = Guid.NewGuid().ToString();
                SessionHelper.Adds("User", new String[] { model.UserName, code }, 300);
                HttpContext.Cache.Insert(model.UserName, code, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(18000));
                auth.text    = "success";
                auth.token   = code;
                auth.isLogin = true;
                var userid = User.Identity.GetUserId <int>();
                auth.PostUsers = db.PostUsers.Include("Post").Where(p => p.UserId == userid).Where(p => p.IsOnDuty == true).ToList();
                return(Json(auth, JsonRequestBehavior.AllowGet));
            }


            case SignInStatus.LockedOut:
            {
                auth.text = "账户被锁定";
                return(Json(auth, JsonRequestBehavior.AllowGet));
            }

            case SignInStatus.RequiresVerification:
            {
                auth.text = "账户需要验证";
                return(Json(auth, JsonRequestBehavior.AllowGet));
            }

            case SignInStatus.Failure:
            default:
            {
                auth.text = "用户名或密码错误";
                return(Json(auth, JsonRequestBehavior.AllowGet));
            }
            }
        }
コード例 #2
0
ファイル: UserManage.aspx.cs プロジェクト: jiequjidian/Touch
    public static List <SysUser> autoLogin()
    {
        List <SysUser> user = null;

        if (loginValidation())//如果已经登录
        {
            string[] currUserInfo = SessionHelper.Gets("CurrLoginUser");
            string   userName     = currUserInfo[0];
            string   password     = currUserInfo[1];
            try
            {
                using (QPCHARTEntities sce = new QPCHARTEntities())
                {
                    string Sql = @"select * from SysUser where YongHu=@p1 and MiMa=@p2";
                    user = sce.Database.SqlQuery <SysUser>(Sql,
                                                           new SqlParameter {
                        ParameterName = "p1", Value = userName
                    },
                                                           new SqlParameter {
                        ParameterName = "p2", Value = password
                    }).ToList();
                }
                if (user.Count <= 0)
                {
                    return(null);
                }

                /*
                 * 写入session
                 * 生命周期为10分钟
                 * */
                string[] userInfo = { user[0].YongHu, user[0].MiMa, user[0].Level.ToString() };
                SessionHelper.Del("CurrLoginUser");
                SessionHelper.Adds("CurrLoginUser", userInfo, 10);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        return(user);
    }
コード例 #3
0
ファイル: UserManage.aspx.cs プロジェクト: jiequjidian/Touch
    public static List <SysUser> login(string userName, string password)
    {
        List <SysUser> user = null;

        password = md5Encode(password);
        userName = userName.Trim();
        try
        {
            /**
             *@QPCHARTEntities : 与数据库链接时对应的实体名称
             * */
            using (QPCHARTEntities sce = new QPCHARTEntities())
            {
                string Sql = @"select * from SysUser where YongHu=@p1 and MiMa=@p2";
                user = sce.Database.SqlQuery <SysUser>(Sql,
                                                       new SqlParameter {
                    ParameterName = "p1", Value = userName
                },
                                                       new SqlParameter {
                    ParameterName = "p2", Value = password
                }).ToList();
            }
            if (user.Count <= 0)
            {
                return(user);
            }

            /*
             * 写入session
             * 生命周期为10分钟
             * */
            string[] userInfo = { user[0].YongHu, user[0].MiMa, user[0].Level.ToString() };
            SessionHelper.Del("CurrLoginUser");
            SessionHelper.Adds("CurrLoginUser", userInfo, 10);
        }
        catch (Exception ex)
        {
            return(null);
        }
        return(user);
    }
コード例 #4
0
ファイル: UserManage.aspx.cs プロジェクト: jiequjidian/Touch
    public static string reg(string userName, string password)
    {
        userName = userName.Trim();
        password = md5Encode(password);
        try
        {
            using (QPCHARTEntities sce = new QPCHARTEntities())
            {
                string Sql   = @"select * from SysUser where YongHu =@p1";
                int    count = sce.Database.SqlQuery <SysUser>(Sql,
                                                               new SqlParameter {
                    ParameterName = "p1", Value = userName
                }).ToList().Count();
                if (count > 0)
                {
                    return("UserExisted");
                }

                Sql = @"insert SysUser(YongHu,MiMa,Level) values('" + userName + "','" + password + "',0)";

                /*
                 * 写入session
                 * 生命周期为10分钟
                 * */
                string[] userInfo = { userName, password, "0" };
                SessionHelper.Del("CurrLoginUser");
                SessionHelper.Adds("CurrLoginUser", userInfo, 10);

                return(sce.Database.ExecuteSqlCommand(Sql) > 0 ? userName : "");
            }
        }
        catch (Exception ex)
        {
            return(null);
        }
    }