asp.net-web-api - 在 ASP.NET Web API GrantResourceOwnerCredentials 中传回参数

标签 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);
}

这就是我连接它的方式:
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/

相关文章:

c# - 可以通过 web api 传递整数列表,还是我必须使用字符串参数

c# - 向 WebAPI 添加新方法会出现 "multiple actions"错误

c# - WebApi.Owin 中的 SuppressDefaultHostAuthentication 也抑制 webapi 外部的身份验证

c# - 如何在用户未登录的情况下保护授权网站?

c# - Azure 网络应用 : Web sockets abort after 100 seconds

javascript - 在按钮上调用操作方法单击 ASP.NET MVC

wcf-web-api - CaSTLe.ActiveRecord.dll 与 ASP.NET MVC 4 Web API 不兼容

c# - 如何在没有托管的情况下启动 ASP.Net 应用程序?

asp.net-web-api - 手动组合请求时,WebAPI RequestBody 中的 QueryString 被截断?

ASP.Net MVC 5 与另一个 MVC 5 Identity 网站的身份验证