Exemple #1
0
        public bool OAuthAccess()
        {
            var app = OAuthAppCache.Get(this._appid);

            if (app == null)
            {
                Alert("无效的应用编号");
                return(false);
            }
            //Tauth_Code daCode = new Tauth_Code();
            var daCode = DaoFactory.Tauth_Code();

            if (!daCode.SelectByAppId_GrantCode(app.Id, this._auth_code))
            {
                Alert("无效的授权码");
                return(false);
            }
            if (daCode.Status == 1)
            {
                Alert("该授权码已被使用,不能重复使用");
                return(false);
            }
            if (daCode.Expire_Time < DateTime.Now)
            {
                Alert("授权码已过期");
                return(false);
            }
            daCode.Status = 1;
            if (!daCode.Update())
            {
                Alert("授权码验证失败");
                return(false);
            }
            var scope = ScopeCache.Get(daCode.Scope_Id);

            BeginTransaction();
            UserTokenProvider utp = new UserTokenProvider(app, daCode.User_Id, daCode, scope.Code);

            utp.ReferenceTransactionFrom(Transaction);
            if (!utp.GenerateUserToken())
            {
                Rollback();
                Alert(utp.PromptInfo);
                return(false);
            }
            this.OAuthUser = utp.OAuthUser;
            if (!UpdateTokenRights(utp.TokenId, utp.Refresh_Timeout, daCode.Right_Json))
            {
                Rollback();
                return(false);
            }
            Commit();
            return(true);
        }
Exemple #2
0
        public bool Login(int client_source, string client_system, string device_id, string ip_address, string session_id, string clientVersion, int appid)
        {
            var fac = UserModuleFactory.GetUserModuleInstance();

            if (fac == null)
            {
                Alert(ResultType.系统异常, "加载用户模块失败");
                return(false);
            }
            try
            {
                UserVoucherType uvt = xUtils.GetVoucherType(this._user_code);
                this.User = fac.GetUserByVoucher(this._user_code, uvt);
            }
            catch (ApplicationException ex)
            {
                Alert(ResultType.非法操作, "无效的登录账号");
                Log.Error($"无效的登录账号[{this._user_code}]", ex);
                return(false);
            }
            catch (Exception exp)
            {
                Log.Error("登录异常", exp);
                Alert(ResultType.系统异常, "系统异常");
                return(false);
            }
            if (this.User == null)
            {
                Alert(ResultType.无效数据类型, "用户未注册");
                return(false);
            }
            if (this.User.Status != UserStatus.已激活)
            {
                Alert(ResultType.非法操作, $"账户{this.User.Status.ToString()}");
                return(false);
            }
            var lockResult = this.User.IsLocked(Winner.User.Interface.Lock.LockRight.登录);

            if (lockResult.IsLocked)
            {
                Alert(ResultType.非法操作, lockResult.Reason);
                return(false);
            }
            if (loginType == LoginType.LOGIN_BY_PASSWORD || loginType == LoginType.密码登录)
            {
                if (!IgnorePassword && !this.User.CheckLoginPassword(_password))
                {
                    Alert(ResultType.非法操作, this.User.ErrorInfo.Message);
                    return(false);
                }
            }
            else if (loginType == LoginType.短信验证码登录)
            {
                SmsValidateProvider smsValid = new SmsValidateProvider(this.User.MobileNo, SmsValidateType.登录验证码);
                if (!smsValid.ValidateCode(_password))
                {
                    Alert(ResultType.非法操作, smsValid.PromptInfo);
                    return(false);
                }
            }
            else
            {
                Alert(ResultType.无效数据类型, "无效的登录方式");
                return(false);
            }
            SaveUserDevice(this.User.UserId, device_id, client_system, client_source);
            //this.Token = xUtils.EncryptAccessToken(this.User.UserId, this.User.UserCode, appid);

            UserTokenProvider utp = new UserTokenProvider(appid, this.User.UserId, null, device_id, this._scope);

            if (!utp.GenerateUserToken())
            {
                Alert(utp.PromptInfo);
                return(false);
            }
            this.OAuthUser = utp.OAuthUser;
            this.Token     = utp.OAuthUser.Token;


            //Tauth_Session daSession = new Tauth_Session
            var daSession = DaoFactory.Tauth_Session();

            daSession.Client_Source  = client_source;
            daSession.Client_System  = client_system;
            daSession.Device_Id      = device_id;
            daSession.Ip_Address     = ip_address;
            daSession.Session_Id     = session_id;
            daSession.Status         = 1;
            daSession.User_Id        = this.User.UserId;
            daSession.Token          = this.Token;
            daSession.Client_Version = clientVersion;

            if (!daSession.Insert())
            {
                Alert(ResultType.系统异常, "保存登录会话失败");
                return(false);
            }
            Logined();
            return(true);
        }
        public bool Refresh()
        {
            var app = OAuthAppCache.Get(this._appid);

            if (app == null)
            {
                Alert(ResultType.非法操作, "未知的应用ID");
                return(false);
            }
            var DecryptRes = UserTokenProvider.DecryptAccessToken(this._refresh_token);

            if (!DecryptRes.Success)
            {
                Alert(ResultType.非法操作, DecryptRes.Message);
                return(false);
            }
            UserToken token = DecryptRes.Content;

            if (token.Expire_Time < DateTime.Now)
            {
                Alert(ResultType.需要登陆, "令牌已过期,请重新发起用户授权");
                return(false);
            }
            //Tauth_Token daToken = new Tauth_Token();
            var daTokenCollection = DaoFactory.Tauth_TokenCollection();

            if (!daTokenCollection.ListByUserId_AppId(token.UserId, app.Id))
            {
                Alert(ResultType.无权限, "未找到授权记录,无效的刷新令牌");
                return(false);
            }
            ITauth_Token daToken = null;

            foreach (ITauth_Token item in daTokenCollection)
            {
                if (item.Refresh_Token.Equals(this._refresh_token))
                {
                    daToken = item;
                }
            }
            if (daToken == null)
            {
                Alert(ResultType.非法操作, "无效的刷新令牌");
                return(false);
            }
            if (daToken.Refresh_Timeout < DateTime.Now)
            {
                Alert(ResultType.无权限, "令牌已过期,请重新发起用户授权");
                return(false);
            }
            var   fac  = UserModuleFactory.GetUserModuleInstance();
            IUser user = fac?.GetUserByID(daToken.User_Id);

            if (user == null)
            {
                Alert("用户不存在");
                return(false);
            }
            string userCode = user.GetUserVoucher(UserVoucherType.自定义号码);
            string newToken = UserTokenProvider.EncryptAccessToken(token.UserId, userCode, app.Id);

            daToken.Token_Code  = newToken;
            daToken.Expire_Time = DateTime.Now.AddSeconds(this.OAuthUser.Expire_In);
            if (!daToken.Update())
            {
                Alert(ResultType.系统异常, "Token刷新失败,请重试");
                return(false);
            }
            this.OAuthUser.Open_Id           = UserTokenProvider.EncryptOpenId(app.Id, token.UserId, userCode, app.UidEncryptKey);
            this.OAuthUser.Token             = newToken;
            this.OAuthUser.Refresh_Token     = this._refresh_token;
            this.OAuthUser.Refresh_Expire_In = (int)(daToken.Refresh_Timeout - DateTime.Now).TotalDays;
            return(true);
        }