场景很简单,我需要从另一台服务器(不同于API服务器)登录才能检索访问令牌.
我Microsoft.Owin.Cors在API服务器上安装了包.在Startup.Auth.cs文件中,public void ConfigureAuth(IAppBuilder app)我加入了
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
Run Code Online (Sandbox Code Playgroud)
在WebApiConfig.cs下面public static void Register(HttpConfiguration config),我添加了以下几行:
// Cors
var cors = new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS");
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)
我还应该改变什么?
在我的应用程序中,我使用带有基于令牌的身份验证的web api和CORS支持,但是当客户端请求令牌时,由于CORS发生错误(跨源请求被阻止:同源策略不允许在(我的站点)读取远程资源这可以通过将资源移动到同一域或启用CORS来解决.)
我已经配置了CORS支持所需的一切(我想是这样).在这里我的配置
Owin开始上课
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration
{
DependencyResolver = new StructureMapWebApiDependencyResolver(container)
};
WebApiConfig.Register(config); // registering web api configuration
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); // cors for owin token pipeline
app.UseWebApi(config);
ConfigureOAuth(app);
}
public void ConfigureOAuth(IAppBuilder app)
{
var oAuthAuthorizationServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(oAuthAuthorizationServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
Run Code Online (Sandbox Code Playgroud)
和我的webapi配置
public static class WebApiConfig
{ …Run Code Online (Sandbox Code Playgroud)