首页 文章

Owin OAuth Context:前端登录发送Json

提问于
浏览
2

我有一个使用OAuth / Owin来处理身份验证的Web API项目 .

当我通过form-urlencoded发布时,一切正常 . 但是前端团队将发布application / json,我无法改变我的方法来接收这个Json .

当我想收到一个Json时,我通常会使用[FromBody],但这次不起作用 .

My code:

public override async Task ValidateClientAuthentication([FromBody]OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
    }

    public override async Task GrantResourceOwnerCredentials([FromBody]OAuthGrantResourceOwnerCredentialsContext context)
    {
        try
        {
            ....
        }
        catch (Exception e)
        {
            context.SetError("invalid_grant", "User not found");
        }
    }
}

My OAuth Config:

public static void ConfigureOAuth(IAppBuilder app, IContainer container)
    {
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true, // HTTPS == false
            TokenEndpointPath = new PathString("/security/login"),
            AccessTokenExpireTimeSpan = TimeSpan.FromHours(2),
            Provider = container.Resolve<IOAuthAuthorizationServerProvider>()                
        };

        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }

Json Example:

{grant_type:“password”,用户名:“myuser”,密码:“mypass”}

1 回答

  • 0

    请阅读请求正文

    context.Request.Body.Position = 0; // this resets the read position to 0
    var payload = await new StreamReader(context.Request.Body).ReadToEndAsync();
    

    从这里你有你的JSON对象的字符串 . 您可以使用反序列化器将其转换为CLR类型 .

相关问题