C# ASP.Net OAuth授权服务器:添加一个数组作为附加响应参数
我已经实现了一个定制的C# ASP.Net OAuth授权服务器:添加一个数组作为附加响应参数,c#,asp.net,oauth-2.0,C#,Asp.net,Oauth 2.0,我已经实现了一个定制的OAuthorizationServerProvider,我想在客户端请求访问令牌时在响应中添加一些额外的元素 为此,我重写了OAuthAuthorizationServerProvider.TokenEndpoint方法,并成功地添加了一些单个元素(通过将它们添加到上下文.AdditionalResponseParameters字典中)。现在我有这样的反应: { "access_token": "wxoCtLSdPXNW9KK09PVhSqYho...", "to
OAuthorizationServerProvider
,我想在客户端请求访问令牌时在响应中添加一些额外的元素
为此,我重写了OAuthAuthorizationServerProvider.TokenEndpoint方法,并成功地添加了一些单个元素(通过将它们添加到上下文.AdditionalResponseParameters
字典中)。现在我有这样的反应:
{
"access_token": "wxoCtLSdPXNW9KK09PVhSqYho...",
"token_type": "bearer",
"expires_in": 1199,
"refresh_token": "uk0kFyj4Q2OufWKt4IzWQHlj...",
"toto": "bloblo",
"tata": "blabla"
}
{
"access_token": "wxoCtLSdPXNW9KK09PVhSqYho...",
"token_type": "bearer",
"expires_in": 1199,
"refresh_token": "uk0kFyj4Q2OufWKt4IzWQHlj...",
"scopes": ["read", "write"]
}
这很好,但我的目标是添加一个数组以获得此类响应:
{
"access_token": "wxoCtLSdPXNW9KK09PVhSqYho...",
"token_type": "bearer",
"expires_in": 1199,
"refresh_token": "uk0kFyj4Q2OufWKt4IzWQHlj...",
"toto": "bloblo",
"tata": "blabla"
}
{
"access_token": "wxoCtLSdPXNW9KK09PVhSqYho...",
"token_type": "bearer",
"expires_in": 1199,
"refresh_token": "uk0kFyj4Q2OufWKt4IzWQHlj...",
"scopes": ["read", "write"]
}
我试图添加一个json解析的列表或数组,而不是一个简单的字符串,但它给了我
"scopes": "[\"read\",\"write\"]"
这是解析为Json的字符串,而不是Json数组:/
如何在TokenEndpoint响应中添加Json数组?问题
当我们使用app.oauthBeareAuthenticationExtensions时,下一个链称为:
public static class OAuthBearerAuthenticationExtensions
{
public static IAppBuilder UseOAuthBearerAuthentication(this IAppBuilder app, OAuthBearerAuthenticationOptions options)
{
if (app == null)
throw new ArgumentNullException(nameof (app));
app.Use((object) typeof (OAuthBearerAuthenticationMiddleware), (object) app, (object) options);
app.UseStageMarker(PipelineStage.Authenticate);
return app;
}
}
然后,类型为OAuthorizationServerMiddleware
的对象使用内部类OAuthorizationServerHandler
,其中使用了JsonTextWriter
:
using (var jsonTextWriter = new JsonTextWriter((TextWriter) new StreamWriter((Stream) memory)))
{
jsonTextWriter.WriteStartObject();
jsonTextWriter.WritePropertyName("access_token");
jsonTextWriter.WriteValue(accessToken);
jsonTextWriter.WritePropertyName("token_type");
jsonTextWriter.WriteValue("bearer");
// and so on
this.Response.ContentLength = new long?((long) body.Length);
await this.Response.WriteAsync(body, this.Request.CallCancelled);
}
这里有两个限制:*)
JsonTextWriter
是一个无法配置的纯类,它只是将字符串作为StringBuilder写入,因此无法应用Json.Settings=new MySettings()
。而且JsontTextWriter
不支持复杂对象。数组只能写为jsonTextWriter.WriteStartArray()
和jsonTextWriter.WriteEndArray()
,但这在OAuthAuthorizationServerHandler中被忽略
*)有些类是内部的,不能被覆盖或继承
Microsoft开发人员似乎没有预见到这个问题,只是将自定义属性限制为IDictionary
解决方案1
使用OAuthBeareAuthentication(…)代替app.UseAuthBeareAuthentication(…)
应用您自己的代码
app.Use<MyOAuthBearerAuthenticationMiddleware>(options);
app.UseStageMarker(PipelineStage.Authenticate);
2) 在身份验证启动方法中使用OAuthBealerTokens之前,请使用新的中间件
app.Use<AuthenticationPermissionsMiddleware>();
app.UseOAuthBearerTokens(options);
app.Use();
应用程序使用OAuthBealerTokens(选项);
这似乎是不可能的,因为处理附加ResponseParameters的方式(每个参数都将使用JsonTextWriter.WriteValue,而此方法只需要数字、GUID、字符串等基本值,而不是数组或对象)。哦,好吧,那么我将找到一种只使用字符串的方法来说明问题。谢谢evkt这是唯一的解决方案,如果你想返回自定义响应,解释得很好。