如何使用 OWIN、OAuth 和 Web API 获取授权码?

How to get authorization code with OWIN, OAuth and Web API?

我关注了这个 post:Token Based Authentication using ASP.NET Web API 2, Owin, and Identity。现在,我有一个 Web API 独立 "server" 能够成功验证用户身份,当我向它发送 username/password 时,我有一个 returns 访问令牌。然后,我可以使用访问令牌访问受保护的数据(在博客中post,我可以访问订单)。

目前,我发送 username/password 以获取访问令牌的客户端是一个控制台应用程序。

我想增加一点复杂性,在获得访问令牌之前,我想获得一个授权码。但是我找不到任何关于如何做的例子。根据我的阅读,我应该发送一个结构如下的 GET 请求:

/authorize?response_type=code&client_id=< ClientID>

这是我在控制台应用程序中所做的:

using (var client = new HttpClient())
{
    var response = await client.GetAsync("http://localhost:63828/authorize?response_type=code&client_id=" + Guid.NewGuid());

    var responseString = response.Content.ReadAsStringAsync().Result;
}

但我收到一条错误消息:

The resource cannot be found.

[HttpException]: The controller for path '/authorize' was not found or does not implement IController. at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

这是 Web API 项目中 Startup.cs 文件的内容:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        ConfigureOAuth(app);

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AuthorizeEndpointPath = new PathString("/authorize"),
            ApplicationCanDisplayErrors = true,
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };

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

我觉得奇怪的是,我定义了“/authorize”端点,但它不可访问...“/token”端点是可访问的,我没有为此做任何特别的事情。

知道如何克服这个问题吗?

使用 Katana 内置的 OAuth2 授权服务器时要记住的一件重要事情是,它的授权端点默认为 pass-through:您必须提供自己的 /authorize 端点(例如使用 MVC 或 Nancy)或直接在 OAuthAuthorizationServerProvider.AuthorizationEndpoint.

中呈现您的同意书

您可以查看 official documentation 的完整演练,了解如何实现您自己的 MVC 控制器和您自己的授权端点。