I want to send JWT Bearer token to the API (ASP.NET Core API) to complete the authorization. I have the following codes:
//get
Future<dynamic> get(String api) async {
var url = Uri.parse(baseUrl + api);
String? token = await SecureStorage().readSecureStorage('token');
var headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": "Bearer $token"
};
var response = await http.get(url, headers: headers);
if (response.statusCode == 200) {
return response;
} else {
print(token);
//Throw Exceptionp
}
}
When I test this code, the status code of the response is still 401. I checked this post but I could not solve it. How can I fix this?
Update: I used jwt.io to decode my token.
The header:
{
"alg": "http://www.w3.org/2001/04/xmldsig-more#hmac-sha512",
"typ": "JWT"
}
The payloads:
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "alex2020",
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "developer",
"UserId": "5",
"exp": 1682964030
}
2
Answers
The problem is that
await SecureStorage().readSecureStorage('token');
creates two"
s in the token string.I solved the problem by removing
"
from token. The complete code is:Since the error is not clear try using single quotes, i remember long time ago faced something similar and it was single quotes that fix it