我关注这篇文章:使用ASP.NET Web API 2,Owin和Identity进行基于令牌的身份验证.现在,我有一个Web API独立"服务器"能够成功验证用户,并在向其发送用户名/密码时返回访问令牌.然后,我可以使用访问令牌来访问受保护的数据(在博客文章中,我可以访问订单).
目前,我发送用户名/密码以获取访问令牌的客户端是一个控制台应用程序.
我想增加一点复杂性,在获得访问令牌之前,我想获得一个授权码.但我找不到任何关于如何做的例子.根据我的阅读,我应该发送一个这样结构的GET请求:
/ authorize?response_type = code&client_id =
这是我在我的控制台应用程序中所做的事情:
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; }
但是我收到一条错误消息:
无法找到该资源.
[HttpException]:未找到路径'/ authorize'的控制器或未实现IController.在System.Web.Mvc.DevaultControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType)中的System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,String controllerName)at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController&controller) System.Web.Mvc上System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback回调,对象状态)的System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态),IControllerFactory&factory) System.Web.HttpApplication.ExecuteStep(IExecutionStep)上的System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()中的.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext上下文,AsyncCallback cb,Object extraData) 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"端点是可访问的,我没有做任何特别的事情.
知道如何克服这个问题吗?