Adding Custom Header to asp.net MVC 4 Web API - asp.net-mvc-4

Is it possible to add custom headers to asp.net MVC 4 Web API?

I assume you are talking about HTTP headers. And I am not sure if you want to customize the response or the request. You can do both. Here is a blog post that shows how to add a custom header in the response using and ActionFilterAttribute. If you are using JQuery ajax to send a request to the Web API you can use the beforeSend event to add a custom header. Here is an example of using it for basic authentication.
$.ajax({
url: _url,
data: _data,
type: _type,
beforeSend: function (xhr) {
var up = username + ":" + password;
xhr.setRequestHeader("Authorization", "Basic " + up);
},
success: _successFunc,
error: _errorFunc
});

Related

Angular-5 StripeConnect -303 Error -Response for Preflight is invalid (Redirect)

I am doing StripeConnect(Standard Account Type) with Angular 5 and Asp.NetCore.
I am able to get redirected to required URL with code={} value.
When I am trying to use this code in below URL,
https://connect.stripe.com/oauth/token\client_secret={{secret test key}}\code={{code}}\grant_type = authorization_code"
1.I get preflight is invalid (Redirect) error in browser from my angular solution.
2.The html for stripe login page from Postman.
The code I wrote for making the post call to above URL (in angular) is :
getCredentialsFromStripe(code: any)
{
let Url = "https://connect.stripe.com/oauth/token";
//\client_secret=" + {{key}} + "\code=" + code + "\grant_type
= authorization_code";
return this.http.post(Url, {
Body: "client_secret = {{key}}\code =" + code,
Headers: {
'Authorization': 'Bearer '+ code,
"Accept": "application/json; charset=UTF-8",
"Access-Control-Allow-Origin": "https://connect.stripe.com/oauth/token"
}
}).map(res => res.json());
}
As per suggestions on internet,I tried making the post call from backend(.NET core(2.0) API for me),but didn't get through in creating account for standard account type.
Moreover testing from postman is giving the full html page of login ,instead of getting this object:
{
"token_type": "bearer",
"stripe_publishable_key": "{PUBLISHABLE_KEY}",
"scope": "read_write",
"livemode": false,
"stripe_user_id": "{ACCOUNT_ID}",
"refresh_token": "{REFRESH_TOKEN}",
"access_token": "{ACCESS_TOKEN}"
}
Can anybody give me a lead on this.I have been stuck past two days.
Any thoughts will be really appreciated.
Well,I was able to fix it. Instead of creating accounts using the URL (given above) in angular project (following the node.js documents),I called the stripe api's in Asp.net core (my api solution) and called them.It worked easily.

How do I use bearer token to navigate to a secured page in ASP.NET Core

I'm trying to learn JWT authentication in ASP.NET Core and have been following some tutorials but have come to a roadblock. What I am trying to do is to get the user to go to a login page and once correct credentials are entered, generate an access_token and then use this token to access the secured page (MVC Action method).
I am able to generate the token and in Postman I am able to access the secured page because I can set the bearer token manually in it once I generate it. My problem is once I'm in the app. I don't know how to set the bearer token so I can navigate to the secured page.
In my login page, I have an Ajax call that does the login and in the success response, I am returning the access_token:
$.ajax({
url: "/Account/Login",
method: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(loginData),
})
.done((response) => {
if (response.Success) {
// access token available here
window.location.href = response.ReturnUrl;
}
else {
//...
}
})
But I am stuck here, I don't know how I can use the access_token to navigate to the secured page. What I am trying to do is after correct login, go to the secured page but this time with the access_token so I can access it.
Where do I do this? How do I do it correctly?

Call PHP rest webservice from ajax MVC asp.net?

i am trying to call php rest webservice from MVC ajax but after calling it it is not going to call success i dont what i am missing here.
webservice required 2 paramaters my code
$.ajax({
type: "POST",
url: "https://test.com/custom/",
dataType: "json",
contentType: "application/json",
data: { "ReleaseVersion": "XXX", "ReleaseType":"XXX" },
success: function (data) {
alert("webservice called successfully");
}
})
it tried using json.stringfy while passing parameters but it wont work.
is anyone how to solve this.
Environment: Visual Studio 2013 and MVC 4.0.
set datatype in ajax call that is jsonp for calling cross domain webservice.
In php webservice code you have to add header before passing json.
as header content type=application/json, then and then only you will get json from php webservice in your ajax call.

Instagram API - How to Setup a Proxy Server for POST Request to Follow a User

I am using the below code to let a user Follow another Instagram user. However, even though the result returns a positive success (meta.code==200), the API is still not allowing a follow.
I now understand that this is because JSONP is using script tags and GET. Is the solution to set up a proxy server? If so, can you demonstrate the code on how to do this?
Many thanks!
$.ajax({
type: "POST",
"method": "POST",
url: 'https://api.instagram.com/v1/users/' + followID + '/relationship?access_token=' + accessToken + '&callback=callbackFunction',
data: {action: 'follow'},/*need this to access the relationship*/
dataType: "jsonp",
success: function(result){
if (result.meta.code == 200) {
document.getElementById(followID).innerHTML = "Requested";
document.getElementById(followID).style.display = "none";
document.getElementById("reqID" + followID).style.display = "block";
alert(result.data.outgoing_status;);
}
}
});
There are the security risks in exposing user access token in javascript
What are the security risks in exposing facebook user access token in javascript?
The access token is a sensible data that must be always invisible.
You should do that through server side (Like PHP, ASP, JSP......)
This is a PHP wrapper for the Instagram API, it may help you
https://github.com/cosenary/Instagram-PHP-API

Cross domain json POST rails 3 rest service

How can i make a cross domain POST call for a rails 3 rest service.
I have read a lot about jsonp but its only for GET method. Are there any other way to do it on rails 3. Can anybody provide me with the exact steps to make a cross domain json POST call to rails rest service.
I want to make a POST call through ajax jquery to rails server side rest service.
I tried using JSONP but it didn't work for me
Include POST as the type.
$.ajax({
type: "POST",
url: 'ruby/file/name',
data: {'foo': 'bar'},
success: function(s) {
console.log('success' + s);
},
error: function(e) {
console.log('error' + e);
}
});