Web API 2 - 为令牌自定义端点

时间:2016-06-22 06:13:24

标签: asp.net asp.net-web-api2

我试图这样做

OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/MyCustomRoutepath/Token"),
        Provider = new ApplicationOAuthProvider(),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
        AllowInsecureHttp = true,
        AuthenticationMode = AuthenticationMode.Active
    };

不确定如何实现这一目标。看起来Web API会自动创建/ token路由。由于某些遗留原因,我无法使用它。我如何实现这一目标?我应该创建新的控制器方法和操作方法,并执行令牌端点应该做的任何事情吗?

请指出正确的方向。

1 个答案:

答案 0 :(得分:0)

您的问题不清楚,但您可以将TokenEndpointPath设置为您想要的自定义令牌路径

// Summary:
//     The request path client applications communicate with directly as part of
//     the OAuth protocol. Must begin with a leading slash, like "/Token". If the
//     client is issued a client_secret, it must be provided to this endpoint.
public PathString TokenEndpointPath { get; set; }

虽然文档以/Token为例,但您可以使用任何所需的路径。无需创建自己的控制器,因为owin中间件将在您指定的路径上处理auth。

var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/MyCustomRoutepath/Token"),
    Provider = new ApplicationOAuthProvider(),
    AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
    AllowInsecureHttp = true,
    AuthenticationMode = AuthenticationMode.Active
};

// Token Generation
app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

参考:Token Based Authentication using ASP.NET Web API 2, Owin, and Identity - Step 9: Add support for OAuth Bearer Tokens Generation