I have a server that running with asp.net. I follow a tutorial from the web that enable my server to issue a token Auth0 2 to the client that request it and created web api so that my android emulator able to retrieve some data from the server. I set my token expired date to 365 days. I try to request a token from Postman by providing grant_type, username and password and as expected server return me a token and I use the Get method from Postman to fetch some data from an API endpoint and submit the token in the header, as expected the server successfully return me the data without any problem. The Postman able to fetch data from the server by using a token issue from yesterday so I assume the token implementation is correct.
Server:
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = true, TokenEndpointPath = new PathString("/token"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(365), Provider = new SimpleAuthorizationServerProvider() }; // Token Generation app.UseOAuthAuthorizationServer(OAuthServerOptions); app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); app.UseOAuthBearerTokens(OAuthServerOptions);
Android react native :Login to get an access token from the server
var formBody="grant_type=password&username="+userEmail+"&password="+userPassword; fetch('http://aaa.aaaa.com/token', { method: 'POST', body: formBody, headers: { //Header Defination'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', },}) .then((response) => response.json()) .then((responseJson) => { //Hide Loader setLoading(false); console.log(responseJson); // If server response message same as Data Matched //if (responseJson.status == 1) if (responseJson.access_token) { global.token=responseJson.access_token; AsyncStorage.setItem('access_token', responseJson.access_token); //console.log(responseJson.data[0].user_id); //navigation.replace('DrawerNavigationRoutes'); navigation.navigate('NavigatorHome'); } else { //AsyncStorage.setItem('user_id', 'test1'); //navigation.navigate('NavigatorHome'); //setErrortext('Please check your email id or password'); console.log('Please check your email id or password'); } }) .catch((error) => { //Hide Loader setLoading(false); console.error(error); });
Fetch Data from API endpoint
var accessToken=global.token; var formBody=""; formBody = JSON.stringify({'module': 'order','action': 'get','value':route.params.orderID }) fetch('http://aaa.aaaa.com/api/Orders?ID='+formBody, { method: 'Get', headers: { //Header Defination Accept: 'application/json', 'Content-Type': 'application/json','Authorization': 'Bearer '+ accessToken, }, }) .then((response) => response.json()) .then((responseJson) => { //Hide Loader //setLoading(false); console.log(responseJson); // If server response message same as Data Matched //if (responseJson.status == 1) }) .catch((error) => { //Hide Loader //setLoading(false); console.error(error); });
After that I try to run with android emulator. First I use the fetch method by providing grant_type, username and password, as expected the server return me a token and I store it with AsyncStorage. Then I try to fetch some data by providing the token I requested previously and server able to return me the data without any problem. But if I leave my emulator like for 15min or 30min, now when I try to fetch data from the server it fail. What I do is I try to request a new token by sending grant_type, username and password again and the new token work as expected.
This is weird! I have double check my access token setting at the server which is 365 days and Postman able to Get data without any problem by using the token that issue yesterday, why did the token that issued to my emulator expired within 15 or 30 min? Hope some body can point out my problem. Thanks in advance!