Beispiel #1
0
        public async Task TestBasic()
        {
            var accessTokenInfo = new AccessToken();
            await _accessTokenRepository.InsertAsync(accessTokenInfo);

            Assert.True(accessTokenInfo.Id > 0);
            var token = accessTokenInfo.Token;

            Assert.False(string.IsNullOrWhiteSpace(token));

            accessTokenInfo = await _accessTokenRepository.GetAsync(accessTokenInfo.Id);

            Assert.NotNull(accessTokenInfo);

            accessTokenInfo.Title = "title";
            var updated = await _accessTokenRepository.UpdateAsync(accessTokenInfo);

            Assert.True(updated);

            await _accessTokenRepository.RegenerateAsync(accessTokenInfo);

            Assert.NotEqual(token, accessTokenInfo.Token);

            var deleted = await _accessTokenRepository.DeleteAsync(accessTokenInfo.Id);

            Assert.True(deleted);
        }
        public async Task <ActionResult <TokensResult> > Submit([FromBody] AccessToken itemObj)
        {
            if (!await _authManager.HasAppPermissionsAsync(Types.AppPermissions.SettingsAdministratorsAccessTokens))
            {
                return(Unauthorized());
            }

            if (itemObj.Id > 0)
            {
                var tokenInfo = await _accessTokenRepository.GetAsync(itemObj.Id);

                if (tokenInfo.Title != itemObj.Title && await _accessTokenRepository.IsTitleExistsAsync(itemObj.Title))
                {
                    return(this.Error("保存失败,已存在相同标题的API密钥!"));
                }

                tokenInfo.Title     = itemObj.Title;
                tokenInfo.AdminName = itemObj.AdminName;
                tokenInfo.Scopes    = itemObj.Scopes;

                await _accessTokenRepository.UpdateAsync(tokenInfo);

                await _authManager.AddAdminLogAsync("修改API密钥", $"Access Token:{tokenInfo.Title}");
            }
            else
            {
                if (await _accessTokenRepository.IsTitleExistsAsync(itemObj.Title))
                {
                    return(this.Error("保存失败,已存在相同标题的API密钥!"));
                }

                var tokenInfo = new AccessToken
                {
                    Title     = itemObj.Title,
                    AdminName = itemObj.AdminName,
                    Scopes    = itemObj.Scopes
                };

                await _accessTokenRepository.InsertAsync(tokenInfo);

                await _authManager.AddAdminLogAsync("新增API密钥", $"Access Token:{tokenInfo.Title}");
            }

            var list = await _accessTokenRepository.GetAccessTokensAsync();

            return(new TokensResult
            {
                Tokens = list
            });
        }