目前,我发送用户名/密码以获取访问令牌的客户端是一个控制台应用程序。
我想增加一些复杂性,在获取访问令牌之前,我想获取授权码。但是我找不到任何关于如何执行此操作的示例。从我所读的内容来看,我应该发送一个结构化的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;
}
但是我收到了一个错误信息:
无法找到该资源。
[HttpException]: 路径“/authorize”的控制器未找到或未实现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”端点是可访问的,而我并没有做任何特殊处理。
你有什么想法可以解决这个问题吗?