我试图在用户登录后从 ASP.NET Web API 传回一些参数。
我的工作基于这个不错的教程:
http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/
例如,我可以在演示页面上看到他发回了 userName。
我创建了自己的从 OAuthAuthorizationServerProvider 继承的提供程序
这就是我所做的:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
....
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim("role", user.Role));
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{
"userName", user.UserName
},
{
"role", user.Role
}
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
}
这就是我连接它的方式:
var OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
var OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
据我了解,AuthenticationProperties 字典应该在 JSON 响应中传递回客户端。但出于某种原因,我没有取回我的附加参数。这就是我得到的:
{"access_token":"G4S1PXdNbtAHLFBo......","token_type":"bearer","expires_in":86399}
我花了很多时间试图解决这个问题,有人能看到我失踪了吗?
最佳答案
我发现了我的问题。好像我误解了属性字典。
我添加了这个方法:
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
context.AdditionalResponseParameters.Add(property.Key, property.Value);
}
return Task.FromResult<object>(null);
}
它基本上采用字典中的条目并将其添加到响应中。我的错误是假设这会为我自动完成。
关于asp.net-web-api - 在 ASP.NET Web API GrantResourceOwnerCredentials 中传回参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239566/