【问题标题】:Web API 2 - customize endpoint for TokenWeb API 2 - 自定义 Token 端点
【发布时间】:2016-06-22 06:13:24
【问题描述】:

我正在尝试这样做

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

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

请指点我正确的方向。

【问题讨论】:

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


    【解决方案1】:

    您的问题不清楚,但您可以将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 中间件将在您指定的路径上处理身份验证。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2016-04-05
      • 2014-03-01
      • 2017-05-08
      • 2018-09-14
      • 1970-01-01
      相关资源
      最近更新 更多