我使用以下默认的webapi ApplicationOAuthProvider
代码登录。我加入
<add name="Access-Control-Allow-Origin" value="*" />
在web.config中,客户端可以通过www.testapi.com/token登录。 一切正常。
但是当我创建自定义webapi功能时。它仍然要求我启用访问源控制。所以我通过在WebapiConfig.cs
EnableCorsAttribute cors = new EnableCorsAttribute("http://www.myweb.com:82", "*", "*");
config.EnableCors(cors);
这次它提示错误说
''Access-Control-Allow-Origin'标头包含多个值“http://www.myweb.com:82,*”,但只允许一个。因此,不允许原点“http://www.myweb.com:82”访问。
所以我删除了web.config中的<add name="Access-Control-Allow-Origin" value="*" />
,它可以正常工作!!。
我返回登录页面,要求添加<add name="Access-Control-Allow-Origin" value="*" />
。但是,如果我添加它。我的webapi方法将无法调用。
如果我不添加。客户端将无法登录。
更新1 的 startup.auth.cs
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Configure the application for OAuth based flow
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true
};
// Enable the application to use bearer tokens to authenticate users
app.UseOAuthBearerTokens(OAuthOptions);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);//as instructed
webapiconfig.cs
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
WebApiConfig.Register(config);
config.EnableCors(new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS, PUT, DELETE"));
//var jsonp = new JsonpMediaTypeFormatter(config.Formatters.JsonFormatter);
//config.Formatters.Insert(0, jsonp);
}
}
答案 0 :(得分:3)
Microsoft.AspNet.WebApi.Cors
nuget package Microsoft.Owin.Cors
nuget package config.EnableCors(new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS, PUT, DELETE"));
添加到WebApiConfig.Register(config);
文件的Startup.cs
行的上方。app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
添加到Startup.Auth.cs
文件中。这必须在致电IAppBuilder.UseWebApi
答案 1 :(得分:1)
好吧最后我设法在“@manprit Singh Sahota”的帮助下完成了工作
我从web.config中删除所有访问策略。
以及WebApiConfig
EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
我只将此行添加到Startup.Auth.cs
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);//working line