在ASP.NET Web API GrantResourceOwnerCredentials中传回参数

Joe*_*son 12 asp.net-web-api owin

我试图在用户登录后从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);
}
Run Code Online (Sandbox Code Playgroud)

这是我把它连接起来的方式:

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);
Run Code Online (Sandbox Code Playgroud)

据我了解,AuthenticationProperties字典应该在JSON响应中传递回客户端.但由于某种原因,我没有得到我的额外参数.这就是我得到的:

{ "的access_token": "...... G4S1PXdNbtAHLFBo", "token_type": "承载", "expires_in":86399}

我花了很多时间试图解决这个问题,有人能看到我失踪了吗?

Joe*_*son 11

我发现了我的问题.好像我误解了属性字典.

我添加了这个方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

它基本上接受字典中的条目并将其添加到响应中.我的错误是假设我会自动完成.