コード例 #1
0
        /// <summary>
        /// 执行授权
        /// </summary>
        /// <returns></returns>
        public bool Grant(bool takeAll, params GrantCodeRight[] rights)
        {
            OAuthApp app = OAuthAppCache.Instance.Find(it => it.APP_CODE.Equals(this._appCode));

            if (app == null)
            {
                Alert("未注册的应用");
                return(false);
            }
            GrantScope[] scope = ScopeCache.Instance.FindAll(this._scope);
            if (scope == null || scope.Length <= 0)
            {
                Alert("未定义的授权类型");
                return(false);
            }
            var   fac  = UserModuleFactory.GetUserModuleInstance();
            IUser user = fac?.GetUserByCode(this._userCode);

            if (user == null)
            {
                Alert("用户信息加载失败");
                return(false);
            }
            if (CheckAlreadyAuth(app.APP_ID, user.UserId))
            {
                return(true);
            }
            if (takeAll && (rights == null || rights.Length <= 0))
            {
                var temp = ScopeRightProvider.GetScopeRights(this._scope);
                rights = new GrantCodeRight[temp.Count];
                for (int i = 0; i < rights.Length; i++)
                {
                    rights[i] = new GrantCodeRight
                    {
                        RightId   = temp[i].Right_Id,
                        RightType = temp[i].Right_Type
                    };
                }
            }
            this.Auth_Code = Guid.NewGuid().ToString("N");
            Tauth_Code daCode = new Tauth_Code();

            daCode.App_Id      = app.APP_ID;
            daCode.Expire_Time = DateTime.Now.AddMinutes(5);
            daCode.Grant_Code  = this.Auth_Code;
            daCode.Scope_Id    = scope.FirstOrDefault().SCOPE_ID;
            daCode.User_Id     = user.UserId;
            daCode.Device_Id   = this._device_id;
            if (rights != null && rights.Length > 0)
            {
                daCode.Right_Json = Javirs.Common.Json.JsonSerializer.JsonSerialize(rights);
            }
            if (!daCode.Insert())
            {
                Alert("授权失败,请重试!");
                return(false);
            }
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// 检查是否已经授权了
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        private bool CheckAlreadyAuth(int appid, int userId)
        {
            Tauth_Code daCode = new Tauth_Code();

            if (!daCode.SelectByAppid_UserId(appid, userId))
            {
                return(false);
            }
            if (daCode.Expire_Time < DateTime.Now)
            {
                return(false);
            }
            if (daCode.Status == 1)
            {
                return(false);
            }
            this.Auth_Code = daCode.Grant_Code;
            return(true);
        }
コード例 #3
0
        public bool OAuthAccess()
        {
            var app = OAuthAppCache.Instance.Find(it => it.APP_CODE.Equals(this._appid));

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

            if (!daCode.SelectByAppId_GrantCode(app.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);
            }
            int   user_id = daCode.User_Id;
            var   fac     = UserModuleFactory.GetUserModuleInstance();
            IUser user    = fac?.GetUserByID(user_id);

            if (user == null)
            {
                Alert("用户不存在");
                return(false);
            }
            string open_id = xUtils.EncryptOpenId(app.APP_ID, user_id, app.UID_ENCRYPT_KEY);

            this.OAuthUser.Open_Id       = open_id;
            this.OAuthUser.Token         = xUtils.EncryptAccessToken(user_id, user.UserCode, app.APP_ID);
            this.OAuthUser.Refresh_Token = xUtils.EncryptAccessToken(user_id, user.UserCode, app.APP_ID, 2592000);
            BeginTransaction();
            Tauth_Token daToken = new Tauth_Token();

            daToken.ReferenceTransactionFrom(Transaction);
            bool exist = daToken.SelectByAppId_UserId(app.APP_ID, user_id);

            daToken.App_Id          = app.APP_ID;
            daToken.Expire_Time     = DateTime.Now.AddSeconds(this.OAuthUser.Expire_In);
            daToken.Refresh_Timeout = DateTime.Now.AddDays(this.OAuthUser.Refresh_Expire_In);
            daToken.Refresh_Token   = this.OAuthUser.Refresh_Token;
            daToken.Token_Code      = this.OAuthUser.Token;
            daToken.Scope_Id        = daCode.Scope_Id;
            daToken.User_Id         = user_id;
            daToken.Grant_Id        = daCode.Auth_Id;
            if (exist)
            {
                if (!daToken.Update())
                {
                    Rollback();
                    Alert("TOKEN生成失败");
                    return(false);
                }
            }
            else
            {
                if (!daToken.Insert())
                {
                    Rollback();
                    Alert("TOKEN生成失败");
                    return(false);
                }
            }
            if (!UpdateTokenRights(daToken.Token_Id, daToken.Refresh_Timeout, daCode.Right_Json))
            {
                Rollback();
                return(false);
            }
            Commit();
            return(true);
        }