c# - Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerProvider - 尝试从 Postman 生成令牌时,clientId 和 clientSecret 为空
问题描述
我正在为我的 asp.net web api 2 应用程序使用 OWIN 安全性,这是我的身份验证启动类设置。
public void ConfigureOAuth(IAppBuilder app)
{
var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new CustomAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
这是CustomAuthorizationServerProvider
类实现,
public class CustomAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.TryGetFormCredentials(out var clientId, out var clientSecret);
if (clientId == "987459827985" && clientSecret == "lkfjldsfjkld")
{
context.Validated(clientId);
}
return base.ValidateClientAuthentication(context);
}
public override Task GrantClientCredentials(OAuthGrantClientCredentialsContext context)
{
var oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, "TestClient"));
var ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties());
context.Validated(ticket);
return base.GrantClientCredentials(context);
}
}
现在,在尝试使用 endpoint 生成令牌http://localhost:8080/token
时,我的 clientId 和 clientSecret 都为 NULL,因此我得到了"error": "invalid_client"
. 我在这里缺少什么?
编辑:编辑
当我raw
用作正文时,我可以看到令牌生成正在工作,并且客户端和秘密都具有价值。为什么它不起作用form-data
?
解决方案
查看邮递员文档:发送 API 请求
最重要的是:
网站表单通常将数据作为 multipart/form-data 发送到 API。您可以使用表单数据正文选项卡在 Postman 中复制此内容。表单数据允许您发送键值对,并指定内容类型。
通过在网络上进行快速搜索,API 需要有一种特殊类型的处理来绑定multipart/form-data
即如何为 multipart/form-data 设置 Web API 控制器
内容类型很重要。
推荐阅读
- python - 如何将破折号应用程序保存为可执行文件以供本地机器使用
- javascript - 将多个对象与对象数组中的值总和合并并在javascript中重新排列数组
- android - Android Fragment - 隐藏永久导航栏打开 BottomSheetFrag 或始终
- c# - 是否有任何工具可以可视化 Akka.net 中的演员关系?
- jquery - (索引):583 未捕获的类型错误:无法读取未定义的属性“添加”
- c# - 如何乘以列值并按另一列分组并使用 LINQ 返回对象列表
- c# - 如何在内存中编译并使用 Microsoft.CodeAnalysis 获取程序集
- json - Newtonsoft.Json.Schema 未针对 JSON 模式正确验证 JSON 输入
- html - Material sidenav 导致 translate 和 flex css 属性出现问题
- ibm-midrange - 如何在记事本电脑上使用 as400 模拟器 tn5250 进行向上/向下翻页