asp.net-web-api
ASP.NET वेब एपीआई कॉर्स सक्षम करना
खोज…
WebAPI 2 के लिए कोर को सक्षम करना
// Global.asax.cs calls this method at application start
public static void Register(HttpConfiguration config)
{
// New code
config.EnableCors();
}
//Enabling CORS for controller after the above registration
[EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
public class TestController : ApiController
{
// Controller methods not shown...
}
वेब एपीआई 2 के लिए विश्व स्तर पर कॉर्स को सक्षम करना
public static void Register(HttpConfiguration config)
{
var corsAttr = new EnableCorsAttribute("http://example.com", "*", "*");
config.EnableCors(corsAttr);
}
सभी डोमेन और विधियों के लिए Asp.Net 5 में कोर को सक्षम करना
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
// ...
}
public void Configure(IApplicationBuilder app)
{
app.UseCors("MyPolicy");
// ...
}
विशिष्ट डोमेन और विधियों के लिए Asp.Net 5 में कोर को सक्षम करना
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddCors();
services.ConfigureCors(options =>
options.AddPolicy("AllowSpecific", p => p.WithOrigins("http://localhost:1233")
.WithMethods("GET")
.WithHeaders("name")));
}
Windows प्रमाणीकरण के साथ WebAPI 2 के लिए CORS कॉन्फ़िगर करें
निम्न सर्वर-साइड कॉन्फ़िगरेशन को CORS अनुरोध को Windows प्रमाणीकरण (कोई अनाम IIS में सक्षम होना चाहिए) के साथ काम करने की अनुमति देता है।
web.config - अप्रभावित (अनाम) प्रीफ़लाइट अनुरोधों को अनुमति दें (विकल्प)
<system.web>
<authentication mode="Windows" />
<authorization>
<allow verbs="OPTIONS" users="*"/>
<deny users="?" />
</authorization>
</system.web>
Global.asax.cs - हेडर के साथ ठीक से उत्तर दें जो कॉलर को किसी अन्य डोमेन से डेटा प्राप्त करने की अनुमति देता है
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (Context.Request.HttpMethod == "OPTIONS")
{
if (Context.Request.Headers["Origin"] != null)
Context.Response.AddHeader("Access-Control-Allow-Origin", Context.Request.Headers["Origin"]);
Context.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, MaxDataServiceVersion");
Context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
Context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
Response.End();
}
}
कोर को सक्षम करना
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// all requests are enabled in this example. SupportsCredentials must be here to allow authenticated requests
var corsAttr = new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true };
config.EnableCors(corsAttr);
}
}
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
वेब एपीआई 2 समापन बिंदु के खिलाफ jQuery से उचित रूप से प्रमाणित अनुरोध भेजें
निम्न उदाहरण दिखाता है कि वेब API 2 के खिलाफ GET और POST दोनों अनुरोधों को ठीक से कैसे बनाया जाए (यदि किसी अन्य डोमेन से भेजा गया है तो CORS को सर्वर-साइड कॉन्फ़िगर किया जाना चाहिए):
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.js"></script>
CORS with Windows Authentication test
<script type="text/javascript">
// GET
$.ajax({
url: "endpoint url here",
type: "GET",
dataType: "json",
xhrFields: {
withCredentials: true
}
})
.done(function (data, extra) {
alert("GET result" + JSON.stringify(data));
})
.fail(function(data, extra) {
});
//POST
$.ajax({
url: "url here",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({testProp: "test value"}),
xhrFields: {
withCredentials: true
},
success: function(data) {
alert("POST success - " + JSON.stringify(data));
}
})
.fail(function(data) {
alert("Post error: " + JSON.stringify(data.data));
});
</script>
सर्वर-साइड कोड:
[System.Web.Http.HttpGet]
[System.Web.Http.Route("GetRequestUsername")]
public HttpResponseMessage GetRequestUsername()
{
var ret = Request.CreateResponse(
HttpStatusCode.OK,
new { Username = SecurityService.GetUsername() });
return ret;
}
[System.Web.Http.HttpPost]
[System.Web.Http.Route("TestPost")]
public HttpResponseMessage TestPost([FromBody] object jsonData)
{
var ret = Request.CreateResponse(
HttpStatusCode.OK,
new { Username = SecurityService.GetUsername() });
return ret;
}
वेब एपीआई 2 समापन बिंदु के खिलाफ AngularJS से उचित रूप से प्रमाणित अनुरोध भेजें
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
CORS with Windows Authentication test (Angular)
<script type="text/javascript">
var app = angular.module('myApp', []);
app.controller('myCtrl', function($http) {
$http(
{
method: 'GET',
url: 'url here',
withCredentials: true,
}
)
.then(function(data) {
alert("Get result = " + JSON.stringify(data.data));
},
function(data, extra) {
alert("Get failed: " + JSON.stringify(data.data));
});
$http(
{
method: 'POST',
url: "url here",
withCredentials: true,
data: { url: "some url", message: "some message", type: "some type"}
}
)
.then(function(data) {
alert("POST success - " + JSON.stringify(data.data));
},
function(data) {
alert("POST failed: " + JSON.stringify(data.data));
});
});
</script>
<div ng-app="myApp" ng-controller="myCtrl">
</div>
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow