时间:2019-03-17 标签:c#asp.netWebAPIOwinJsonToken

标签 c# asp.net json asp.net-web-api asp.net-identity

嘿,我刚刚编写了一个基于 token 的身份验证,如下 tutorial 。 因此,只要我将 POST 请求作为 x-www-form-urlencoded 发送,一切都会顺利。所以现在我的队友需要用json来获取 token ,但他得到的只是“不支持的grant_type”。那么我可以更改 token 的可接受类型还是必须寻找其他解决方案?

我的配置如下:

public void Configuration(IAppBuilder app)
    {
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        var myProvider = new MyAuthorizationServerProvider();
        OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = myProvider
        };

        app.UseOAuthAuthorizationServer(options);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);
        }
    }

这就是我的请求的样子,请记住这不适用于 json This is how my request look like keep in mind this doesnt work with json 对于 JSON 它不起作用: enter image description here 最好的问候:)

最佳答案

使用 application/x-www-form-urlencoded 作为 Content-Type 的原因很简单:OAuth2 规范 (RFC 6749) 要求 token 请求使用此内容类型。

任何其他内容类型都会破坏 OAuth2 兼容的客户端兼容性。我建议您不要改变这个标准行为。

来自 Microsoft.Owin.Security.OAuthOAuthAuthorizationServerMiddleware (更准确地说是内部使用的 OAuthAuthorizationServerHandler )的默认实现只是忽略 Content-Type header 并尝试将请求正文作为表单读取。

另一种方式 ,在RequestBody中你可以写, grant_type=password&username=yourUserName&password=MyPassword123,

还要确保 grant_type=password&username=username&password=password 之后没有空格或换行符。

enter image description here

关于时间:2019-03-17 标签:c#asp.netWebAPIOwinJsonToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42504476/

相关文章:

c# - FromBody 收到的帖子导致可序列化错误

c# - 无法将 mysql 查询转换为 linq 语句

c# - C# 中任意代码块(通过大括号)的实际用途是什么

.net - ASP.NET WebForm 的 'page lifecycle' 是什么?

c# - 如何避免加载不必要的程序集

python - 将 CSV 转换为 JSON。如何保持具有相同索引的值?

c# - EWS 搜索过滤器查找发件人域 C#

c# - ViewStateUserKey + 共享主机 + ViewStateMac 验证失败

json - Jackson 反序列化递归对象

ios - 从 JSON 中获取对象