我有一个Web表单解决方案,我尝试添加一个自托管的API项目来支持移动请求的交互(使用http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/作为我的引用点)。但是,在配置我的OAuth" TokenEndpointPath"我收到了404响应 - 仅限于我在discountasp.net托管的生产环境中。在本地,它完美无缺。
在我的网站项目中,我有一个global.asax,它有一个空的Application_Start()方法。在我的API项目中,我在Startup.cs中有这个:
[assembly: OwinStartup(typeof(MyDomain.API.Startup))]
namespace MyDomain.API
{
public class Startup
{
public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
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)
{
//use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new SimpleAuthorizationServerProvider(),
RefreshTokenProvider = new SimpleRefreshTokenProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
}
}
}
这是我的API项目的App_Start / WebApiConfig.cs:
namespace MyDomain.API
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
}
我不确定您可能需要哪些其他信息,但有人知道为什么我在尝试发布到http://mydomain/token时会获得404吗?要明确的是,我的网站项目是返回404响应的项目(我得到的是自定义404页面的HTML输出...不是JSON格式的服务器消息或类似的东西)。