Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to authenticate .net core web service via anuglar 7 application. I have tried passing
application/json
but for some reason the request isn't received by the service.

public async Task Invoke(HttpContext context)
        {  
            string authHeader = context.Request.Headers["Authorization"];
            if (!string.IsNullOrEmpty(authHeader))
            {
                var secretKey = Startup.AppSettings.Secret;
                var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey));
                List<SymmetricSecurityKey> keys = new List<SymmetricSecurityKey>();
                keys.Add(signingKey);

                try
                {
                    ValidateAndDecode(authHeader, keys);
                    await _next.Invoke(context); 
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = 401;
                    await context.Response.WriteAsync(ex.Message);
                }
            } 
        }


intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        const authReq = request.clone({
            headers: new HttpHeaders({
                //'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
                'Content-Type': 'application/json'
            })
          });
        
          console.log('Intercepted HTTP call', authReq);
        
          return next.handle(authReq);
       
    }


request is received when I send headers as
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'

but change it to
'Content-Type': 'application/json'

and service stops getting request from client

What I have tried:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

       const authReq = request.clone({
           headers: new HttpHeaders({
               //'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
               'Content-Type': 'application/json'
           })
         });

         console.log('Intercepted HTTP call', authReq);

         return next.handle(authReq);

   }


Added headers but didn't work
Posted
Comments
Safeena Shabeer 22-Apr-19 4:42am    
Did you configure CORS in your service application?
Qadeer Ahmed Khan 18-Mar-20 10:27am    
Thanks. Yes it worked after configuring CORS.
Christian Graus 22-Apr-19 17:41pm    
Where does this function live, is it some sort of custom auth middle ware? Is the call getting through to your controller?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900