我对使用Discord API与Discord接口感兴趣.我会将他们的文档描述为"稀疏",但也许我只是没有找到正确的位置.我的大多数信息来自此页面:
https://discordapp.com/developers/docs/topics/oauth2
我已经设置了我的Discord公会和应用程序(甚至是机器人,这可能是不必要的).我的具体计划是允许用户授予我的网站权限,将其添加到私人Discord公会/服务器.我在我的网站页面上有一个引用此URL的超链接:
这部分似乎运作良好.用户批准该请求.然后,用户将在查询字符串中使用"代码"键值对发送回我的站点.我认为这段代码就是所谓的"授权代码".那么如何使用此授权码将用户添加到我的公会?我在Discord网站上找到了这个页面:
https://discordapp.com/developers/docs/resources/guild#add-guild-member
从该页面我可以看到我需要启动此URL的PUT:
https://discordapp.com/api/guilds/ {guild.id}/members/{user.id}
但我不知道{user.id}.我只有一个授权码.
它还说,"...如果你拥有guilds.join范围的用户,你有一个有效的oauth2访问令牌." 我没有访问令牌.同样,我只有一个授权码.
所以在我看来,我需要以某种方式交换此授权码以获取访问令牌和用户ID.有人能告诉我该怎么做吗?我一直在尝试使用以下URL,但我不知道使用什么方法(GET,POST等)或发送它的参数:
https://discordapp.com/api/oauth2/token
因为我想了解其工作方式的内在性,我更愿意知道如何使用普通的Web请求(例如HttpWebRequest和WebClient,而不是使用某些OAuth库).
我决定阅读(有选择地)这个RFC:
https://tools.ietf.org/html/rfc6749#section-4.1.3
我已将我认为最合适的部分联系起来.似乎正确的过程是向以下URL和参数发送POST请求:
https://discordapp.com/api/oauth2/token
grant_type = authorization_code&代码= [AuthorizationCode]&REDIRECT_URI = [RedirectURI]&CLIENT_ID = [客户端ID]
这似乎也符合彼得G的答案.不幸的是,此请求因401(未授权)错误而失败.所以我认为这是一个死路一条.我已经尝试了几次,但希望有一个解决方案.我收到了这个回复机构:
{"error":"invalid_client"}
我收到了这些标题:
连接:关闭
Pragma:没有缓存
严格运输安全:max-age = 31536000; includeSubDomains
Alt-Svc:清楚
CF-RAY:[RedactedJustInCase]
内容长度:27
缓存控制:无存储
Content-Type:application/json
日期:2017年4月7日星期五01:12:19 GMT
Set-Cookie:__ cffidid = [RedactedJustInCase]; expires =周六,07-Apr-18 01:12:19 GMT; 路径= /; 域= .discordapp.com; 仅Http
服务器:cloudflare-nginx
通过:1.1谷歌
我是 Discord 的 API 的新手,我正在处理一个需要能够以编程方式添加公会成员的项目。我已经学会了如何获取授权代码(带有标识和 guilds.join 范围),将其兑换为访问令牌,并获取用户 ID。最后一步是使用访问代码和用户ID添加公会。这个命令在这里有详细说明:
https://discordapp.com/developers/docs/resources/guild#add-guild-member
看来我需要向这个 URL 发送一个 PUT 请求:
https://discordapp.com/api/guilds/[GuildID]/members/[UserID]
但这会导致以下响应:
{"code": 0, "message": "401: Unauthorized"}
我尝试在授权标头中包含访问令牌:
授权:承载 [已编辑]
我还尝试向请求添加 JSON 正文:
{"access_token":"[已编辑]"}
两者都没有奏效。不出所料,同时使用两者也不起作用。
我想知道这是否是权限问题,但 Discord 确认我有 guilds.join 范围。这是我在为访问令牌交换授权码时收到的 JSON:
{"access_token": "[Redacted]", "token_type": "Bearer", "expires_in": 604800, "refresh_token": "[Redacted]", "scope": "identify guilds.join"}
识别范围有效,因为我能够检索用户及其 ID。但是 guilds.join 似乎不起作用。
我在下面包含了一些测试代码。我已标记“选项 1”和“选项 2”行以表示我通常不会在同一请求中同时使用这两种访问代码方法。但正如我之前提到的,我确实尝试了两者,但仍然出现 401 错误。
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
client.Headers.Add(HttpRequestHeader.Authorization, "Bearer [Redacted]");//Option 1
string output = client.UploadString
(
"https://discordapp.com/api/guilds/[GuildID]/members/[UserID]",
WebRequestMethods.Http.Put,
"{\"access_token\":\"[Redacted]\"}"//Option 2
);
}
Run Code Online (Sandbox Code Playgroud)
因为我想了解它是如何工作的,所以我更想知道如何使用普通的 …
我有一个相对较新的ASP.NET Core 2站点.它只在一台服务器上运行(Windows Server 2012 R2,IIS 8.5),每上几天我上传更新时只会重启一次.大约每天一次,由于防伪系统的拒绝,用户的请求失败.这些是POST请求,并没有什么特别之处.我在POST请求中包含了防伪值,99%的时间,POST请求都有效.但是当它们没有时,stdout日志会说,"防伪令牌验证失败.防伪cookie令牌和请求令牌不匹配." 当我使用该确切语句执行Web搜索时,我得到零结果.所以我转向Stack Overflow.[这已不再适用,因为Web搜索现在会产生此Stack Overflow问题.]
我已经在下面列出了stdout日志的相关部分.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 POST [domain redacted] application/x-www-form-urlencoded 234
info: Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ValidateAntiforgeryTokenAuthorizationFilter[1]
Antiforgery token validation failed. The antiforgery cookie token and request token do not match.
Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery cookie token and request token do not match.
at Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.ValidateTokens(HttpContext httpContext, AntiforgeryTokenSet antiforgeryTokenSet)
at Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.<ValidateRequestAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ValidateAntiforgeryTokenAuthorizationFilter.<OnAuthorizationAsync>d__3.MoveNext()
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[3]
Authorization …Run Code Online (Sandbox Code Playgroud) 我有一个由过去的开发人员执行过编程的客户。他们的代码最近变得可疑,我想知道他们是否在使用参数化查询。我希望可以通过SQL Server检测到非参数化的请求,但是我还没有找到一种方法来执行此操作。我了解并非所有查询都需要参数化,因为查询可能类似于
select count(*) from Customers
Run Code Online (Sandbox Code Playgroud)
但是,如果SQL Server可以通过编程方式告诉我哪些查询具有任何字符串常量输入而不是参数输入,那就太好了。顺便说一句,关闭所有SQL查询并用@符号删除所有行的操作已关闭,但是下面的查询将被视为合法:
select * from Users where Username='user' and Password=@Password
Run Code Online (Sandbox Code Playgroud)
因此,我确实需要SQL Server读入命令的内容并确定是否所有输入都已参数化。谢谢。
sql sql-server security parameterization parameterized-query
我刚刚开始使用 .NET Standard,我正在尝试掌握这个新场景中的最佳实践。我已将一些关键库转换为 .NET Standard 2.0。当我创建一个新的 Web 应用程序(ASP.NET 4.6.1、4.6.2 或 4.7)项目,添加新库作为引用,然后进行构建时,我收到很多警告。如果库面向 .NET Standard 1.4 或更早版本,则不会显示这些警告,但我需要 .NET Standard 2.0。这是这些警告的一部分:
Consider app.config remapping of assembly "System.Runtime.Serialization.Xml, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" from Version "4.0.10.0" [C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\System.Runtime.Serialization.Xml.dll] to Version "4.1.3.0" [C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\\net461\ref\System.Runtime.Serialization.Xml.dll] to solve conflict and get rid of warning.
... [About 50 similar lines] ...
Consider app.config remapping of assembly "System.Runtime.InteropServices.RuntimeInformation, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" from Version "0.0.0.0" [] to Version "4.0.2.0" [C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\\net461\ref\System.Runtime.InteropServices.RuntimeInformation.dll] to solve conflict and get …Run Code Online (Sandbox Code Playgroud) 假设我有一个ASCX用户控件,需要访问当前用户的全名.ASPX页面顶部包含此行
<%@ Register src="top.ascx" tagprefix="custom" tagname="top" %>
Run Code Online (Sandbox Code Playgroud)
而这一行在体内:
<custom:top runat="server" />
Run Code Online (Sandbox Code Playgroud)
ASPX文件知道当前用户的用户ID,并可以确定他的全名.那么如何使用ASPX文件运行的代码将其信息提供给ASCX文件呢?
我正在用C#和XAML编写一个Windows 8应用程序,它在WebView中托管一个Web页面.Web页面调用alert()和confirm(),但WebView抛出异常,说"'alert'未定义,"或"'confirm'未定义." 可以启用alert()和confirm(),还是需要编写代码来模拟其正常功能?如果我需要自己写,我该如何开始这样的事业呢?谢谢.
请参阅以下两种方法。第一个返回一个IAsyncEnumerable. 第二个尝试消耗它。
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
public static class SqlUtility
{
public static async IAsyncEnumerable<IDataRecord> GetRecordsAsync(
string connectionString, SqlParameter[] parameters, string commandText,
[EnumeratorCancellation]CancellationToken cancellationToken)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
using (SqlCommand command = new SqlCommand(commandText, connection))
{
command.Parameters.AddRange(parameters);
using (var reader = await command.ExecuteReaderAsync()
.ConfigureAwait(false))
{
while (await reader.ReadAsync().ConfigureAwait(false))
{
yield return reader;
}
}
}
}
}
public static async Task Example() …Run Code Online (Sandbox Code Playgroud) 我有一个需要显式类型参数的 C# 方法,但收到此错误:
error CS0411: The type arguments for method 'Test.TargetMethod<TObjectFactory, TResult>(TObjectFactory)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
我通常可以通过指定适当的参数(常规参数,而不是类型参数)来解决这个问题,但在这种情况下,我可能必须明确声明类型参数。但我不想指定类型参数,因为它会增加冗长。
TargetMethod请参阅下面的示例代码,特别是底部附近的两个调用。在第一次调用 时TargetMethod,类型参数应该很明显,因为我传入了 a StringFactory,它只返回 a string。(请参阅 的泛型类型约束TargetMethod。)有没有办法让 C# 意识到它拥有它需要知道的所有内容并且不需要类型参数?我想知道是否需要更改签名,TargetMethod以便 C# 可以理解发生了什么。谢谢。
public interface IObjectFactory<out TResult>
{
TResult Construct();
}
public class StringFactory : IObjectFactory<string>
{
public string Construct() { return ""; }
}
public static class Test
{
//I need to be able to pass in …Run Code Online (Sandbox Code Playgroud) c# ×7
.net ×3
asp.net ×2
discord ×2
oauth ×2
oauth-2.0 ×2
ascx ×1
asp.net-core ×1
async-await ×1
csrf ×1
iis ×1
javascript ×1
security ×1
sql ×1
sql-server ×1
webview ×1
windows-8 ×1
xaml ×1