【发布时间】:2020-04-28 06:36:03
【问题描述】:
我正在为我的 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);
}
}
现在,在尝试使用端点 http://localhost:8080/token 生成令牌时,我的 clientId 和 clientSecret 都为 NULL,因此我得到了 "error": "invalid_client"。我在这里缺少什么?
编辑:编辑
当我使用raw 作为正文时,我可以看到令牌生成正在工作,并且客户端和机密都具有价值。为什么它不适用于form-data?
【问题讨论】:
-
资源所有者密码凭据不提供客户端 ID。请看这个链接:codeproject.com/Articles/1187872/…
-
但我正在使用客户端凭据流
标签: c# asp.net-web-api2 owin