And*_*NET 17 cors asp.net-web-api angularjs owin asp.net-web-api2
我正在为我的API设置一个WebAPI端点,但是我无法使用我的PostRegister方法调用AngularJS.
webAPI和Angular位于不同的站点上.
在Startup.cs我有:
public void Configuration(IAppBuilder app)
{
ConfigureOAuth(app);
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
private void ConfigureOAuth(IAppBuilder app)
{
var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString(Paths.TokenPath),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = Kernel.Get<IOAuthAuthorizationServerProvider>()
};
app.UseOAuthAuthorizationServer(oAuthServerOptions);
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
}
Run Code Online (Sandbox Code Playgroud)
然后在控制器上,我的方法被设置为允许匿名:
[AllowAnonymous]
public bool PostRegister(UserSignupForm form)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我还更新了web.config:
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
在Angular方面,我做了一个简单的$ http.post调用:
saveRegistration: function (registration) {
return $http.post(baseUrl + '/api/signup/register', registration).then(function (response) {
return response;
});
}
Run Code Online (Sandbox Code Playgroud)
当使用Postman进行调用时,我得到了一个成功的响应,但是当从Angular执行相同的调用时,我得到405错误.

BitOfTech示例站点正在发送相同的请求标头,但能够获取Access-Control-Allow-XXX标头

我已经使用ASP.NET Web API 2,Owin和Identity更新了我的代码以遵循基于令牌的身份验证
Ser*_*diy 10
I had similar issue recently. I added controller handler for the pre-flight OPTIONS request and it solved the problem.
[AllowAnonymous]
[Route("Register")]
[AcceptVerbs("OPTIONS")]
public async Task<IHttpActionResult> Register()
{
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13931 次 |
| 最近记录: |