使用ASP Identity 2 POST到/ Token端点时始终收到'invalid_client'错误

Hay*_*fee 19 c# asp.net asp.net-web-api asp.net-identity asp.net-web-api2

大约一个月前,我有一个项目与ASP Identity OAuth完美配合.我将使用grant_type,username和password向/ Token端点发送POST请求,所有这些都是花花公子.

我最近开始了一个基于Visual Studio 2013 RC2的SPA模板的新项目.它与旧模板有点不同.身份验证设置为非常基本的默认值,

OAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    //AuthorizeEndpointPath = new PathString("/Account/Authorize"), 
    Provider = new ApplicationOAuthProvider(PublicClientId),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp = true
};
Run Code Online (Sandbox Code Playgroud)

默认模板没有任何重大变化.我可以通过我实现的Web API控制器方法成功注册帐户;

    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new TunrUser() { UserName = model.Email, Email = model.Email, DisplayName = model.DisplayName };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                return Created(new Uri("/api/Users/" + user.Id,UriKind.Relative), user.toViewModel());
            }
            else
            {
                return BadRequest(result.Errors.First());
            }
        }
        return BadRequest(ModelState);
    }
Run Code Online (Sandbox Code Playgroud)

但是,无论我向/ Token端点发布什么,我都会得到相同的响应.

{"error":"invalid_client"}
Run Code Online (Sandbox Code Playgroud)

通常我会传递以下请求正文

grant_type=password&username=user%40domain.com&password=userpassword
Run Code Online (Sandbox Code Playgroud)

但这会导致同样的错误.这适用于之前的VS2013 SPA模板/身份.改变了什么?

谢谢!

Kja*_*son 16

您必须覆盖OAuthAuthorizationServerProvider中的ValidateClientAuthentication和GrantResourceOwnerCredentials.

请参见此处的示例:http: //www.tugberkugurlu.com/archive/simple-oauth-server-implementing-a-simple-oauth-server-with-katana-oauth-authorization-server-components-part-1


Hay*_*fee 3

因此,事实证明,新模板不包含旧模板中存在的 ApplicationOAuthProvider 的功能实现。

观看此构建演讲后,我进一步调查并发现可以在此 NuGet 包中查看 ApplicationOAuthProvider 的工作实现!它与旧的实现非常相似。