Exemplo n.º 1
0
        /// <summary>
        /// 登录的公共方法
        /// </summary>
        /// <param name="LoginModel">登录数据实体,包括用户名、密码、记住我等信息</param>
        /// <returns>登录状态的枚举</returns>
        /// <exception cref="Jurassic.AppCenter.JException">JException</exception>
        public LoginState Login(LoginModel model)
        {
            if (StateProvider == null)
            {
                throw new JException("没有提供StateProvider,请提供实现了IStateProvider接口的登录提供程序。");
            }

            var result = StateProvider.Login(model);

            if (result == LoginState.OK)
            {
                AppUser user = UserManager.GetByName(model.UserName);


                if (user == null)
                {
                    if (UserProvider.GetType() == typeof(UserDataProvider))
                    {
                        user = new AppUser()
                        {
                            Name = model.UserName
                        };
                        UserManager.Add(user);
                    }
                    else //当使用外部提供程序时,找不到用户则重新读取全部用户数据
                    {
                        UserManager.Clear();
                        user = UserManager.GetByName(model.UserName);
                    }
                    if (user == null)
                    {
                        throw new JException("User Not Exists:" + model.UserName);
                    }
                }
                user.LastOpTime = TimeHelper.ServerTime;
                user.IsOnline   = true;
                if (HttpContext.Current != null)
                {
                    if (!StateProvider.SetAuthCookie(model))
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    }
                }
            }

            return(result);
        }