I am working on getting an authentication token from my web API. I am taking code from my Angular web application that is calling the API and retrieving the token successful. I have put my code in a service that I have moved over to my ionic application.
var _login = function (loginData) {
var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
if (loginData.useRefreshTokens) {
data = data + "&client_id=" + ngAuthSettings.clientId;
}
var deferred = $q.defer();
$http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {
if (loginData.useRefreshTokens) {
//angular version commented and replaced with local storage for ionic
//localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName, fullName: loginData.fullName, refreshToken: response.refresh_token, useRefreshTokens: true });
window.localStorage['authorizationData'] = JSON.stringify({ token: response.access_token, userName: loginData.userName, fullName: loginData.fullName, refreshToken: response.refresh_token, useRefreshTokens: true });
}
else {
//angular version commented and replaced with local storage for ionic
//localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName, fullName: loginData.fullName, refreshToken: "", useRefreshTokens: false });
window.localStorage['authorizationData'] = JSON.stringify({ token: response.access_token, userName: loginData.userName, fullName: loginData.fullName, refreshToken: "", useRefreshTokens: false });
}
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
_authentication.fullName = loginData.fullName;
_authentication.useRefreshTokens = loginData.useRefreshTokens;
deferred.resolve(response);
}).error(function (err, status) {
_logOut();
deferred.reject(err);
});
return deferred.promise;
};
The issue I have, is that while this works fine in angular, it does not work in ionic. I am getting the error back that this is an unsupported grant type. When I inspect the request in fiddler, it is also different, leading me to believe that there is something I need to do differently in this code for it it work in ionic. Here are the two raw requests:
Angular’s Request
POST http://localhost:26264/token HTTP/1.1
Host: localhost:26264
Connection: keep-alive
Content-Length: 53
Accept: application/json, text/plain, */*
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2500.0 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:32150/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
grant_type=password&username=myusername&password=mypw
Ionic Request
OPTIONS http://localhost:26264/token HTTP/1.1
Host: localhost:26264
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4
Access-Control-Request-Headers: accept, authorization, content-type
Accept: */*
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
I notice that in Ionic’s request it doesn’t even seem to send the data to the post.
So, any assistance would be greatly appreciated!
Thanks!