In my service .ts file i coded like this
getUsers(): Observable<any> {
let httpOptions = new HttpHeaders().set('Authorization', 'Bearer ' +
this.securityService.securityObject.bearerToken);
return this.http.get(environment.baseApiUrl + "user", { headers: httpOptions });
}
In the bearerToken we have token value, but the headers showing in the httpOptions is like below image. (took from the console)
in my controller class i have
[HttpGet]
[Authorize]
public async Task<IActionResult> Get()
{
In my HttpInterceptor i have written like
var token = localStorage.getItem("bearerToken");
if (token) {
const newReq = request.clone(
{
headers: request.headers
.set('Authorization',
'Bearer ' + token)
});
return next.handle(newReq);
}
else {
return next.handle(request);
}
my local storage have my token value, but
When i debug the code what i can see is its not entering inside the clone is this is the reason for headers not any entries like my image. Is clone ok to use there?
These were the main culprits which caused my issue
Like corsaro mentioned interceptors will take care our http request no need to mention it separately
In my startup.cs file i was using before like this
app.UseAuthorization();
app.UseAuthentication();
I swapped it's order to
app.UseAuthentication();
app.UseAuthorization();
In my interceptor class i modified header section to this
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization':'Bearer ' + token
})
Copied JwtSettings which declared in the appsettings.json file to appsettings.development.json file for local debug
Now after making these changes its working fine now :)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments