Пример #1
0
        public ActionResult LogOn(Object model, string returnUrl)
        {
            ///* 每次登录时首先检测License,如果未注册、过期、不可用则直接跳到License注册页面,
            //* 通过当前机器码,用注册机生成对应的lincense输入注册,即可完成注册
            //*/
            //if (!Gta.Dtp.MvcUI.Controllers.LicenseController.CheckRegisterInfo())
            //{
            //    //return  RedirectToAction("Index", "License");
            //    return Json(new ResultMessage() { Message = "您还未注册!", Success = false, IsTeacher = false });
            //}
            try
            {

                if (!ModelState.IsValid)
                {
                    return Json(new ResultMessage() { Message = "您输入的信息不完整或者有错误,请检查!", Success = false });
                }

                string userName = null;
                string password = null;

                //去掉空格
                if (!string.IsNullOrEmpty(userName))
                    userName = Server.UrlDecode(userName).Trim();
                //密码不能去掉空格
                if (!string.IsNullOrEmpty(password))
                    password = HttpContext.Server.UrlDecode(password);

                userinfo user = null;
                    //_userinfoBll.GetUserInfo(userName, Encrypt.MD5(password));

                //将用户信息添加至验证票,加密保存至cookie中
                WebContext webContext = new WebContext(this.HttpContext);
                UserData userData = new UserData()
                {
                    //UserID = user.UserID,
                    //UserName = userName,
                    //FullName = user.FullName,
                    //RoleID = user.RoleID,
                    //ResourceclassId = user.ResourceClassID
                };
                webContext.UserLogin(userName, userData);

                if (Url.IsLocalUrl(returnUrl) &&
                    returnUrl.Length > 1 &&
                    returnUrl.StartsWith("/") &&
                    !returnUrl.StartsWith("//") &&
                    !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }

            }
            catch (Exception ex)
            {
                //Gta.Dtp.MvcUI.MvcApplication.LogInfo.Info("\n\n" + ex.Message);
                throw ex;
            }
            return null;
        }
Пример #2
0
        public void UserLogin(String userName, UserData userData = null)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                2,
                userName,
                DateTime.Now,
                DateTime.Now.AddMinutes(CookieExpiration_Minute),
                !ClearCookiOnCloseBrowser,
                userData == null ? "" : userData.ToString(),
                FormsAuthentication.FormsCookiePath
            );

            string ticString = FormsAuthentication.Encrypt(ticket);

            //把票据信息写入Cookie和Session
            //SetAuthCookie方法用于标识用户的Identity状态为true
            HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, ticString));
            //FormsAuthentication.SetAuthCookie(userName, true);
            HttpContext.Current.Session["USER_LOGON_TICKET"] = ticString;
            HttpContext.Current.Session["USER_LOGON_USERDATA"] = userData;

            //重写HttpContext中的用户身份,可以封装自定义角色数据;
            //判断是否合法用户,可以检查:HttpContext.User.Identity.IsAuthenticated的属性值
            string[] roles = new string[] { userData.RoleID.ToString() };
            IIdentity identity = new FormsIdentity(ticket);
            IPrincipal principal = new GenericPrincipal(identity, roles);
            HttpContext.Current.User = principal;
        }