2

I am new to Angular 2 + NodeJS. I come from .NET. In .NET, I know you would just get the request object and pull the server variables out but in angular 2 it seems you have to some how get the request and pull the header variables out.

My company has its own SSO, I need to get the header variables it adds before coming to my application.

How do I get the header variables that are passed to my application? Is there a standard way to get it in Angular 2?

When I was trying to research it myself all I kept getting a lot of "set header" situations

imGreg
  • 900
  • 9
  • 24
  • I do not believe you can achieve it thru client side JS. See discussion here: http://stackoverflow.com/questions/220149/how-do-i-access-the-http-request-header-fields-via-javascript – wannadream May 02 '17 at 03:43
  • I would think it would be possible to retrieve the headers if you can set the headers – imGreg May 02 '17 at 17:39
  • See this as well. http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript However, these solutions require you to send a XMLHttpRequest first, not your case. – wannadream May 02 '17 at 17:45
  • Yea I saw that. Cant seem to find a solution for when its redirected to me from an external website – imGreg May 02 '17 at 17:47

2 Answers2

0

I am thinking of an alternative solution for you. See my graph.enter image description here

Since you mentioned .NET, use MVC here. https://www.asp.net/mvc In controller, you can do something like this:

    public ActionResult SsoMiddleware()
    {
        string[] keys = Request.Headers.AllKeys;
        string authString = "";
        foreach (var key in keys)
        {
            if (key == "SSO Authentication String")
                authString = Request.Headers[key];
        }

        return View(authString);
    }

In your view SsoMiddleware.cshtml, add Javascript:

    @model string

    ...

    <script type="text/javascript">
        localStorage.setItem('auth-string', '@(Model)');
        window.location.href = 'angular-app.html';
    </script>
wannadream
  • 1,669
  • 1
  • 12
  • 14
  • Well, even with this solution. I still dont know how to get the header information or where to get the header from the external site. – imGreg May 02 '17 at 18:45
  • The details in my question maybe misleading. I am using angular 2 with nodejs though my background is .NET. I am not using .NET + Angular 2 – imGreg May 02 '17 at 19:02
  • what library are you using as HTTP service? – wannadream May 02 '17 at 19:04
  • Do you mean the the http server ? – imGreg May 02 '17 at 19:05
  • On my computer i am using Angular CLI to run. When it being hosted, its going on to PCF with http-server-spa – imGreg May 02 '17 at 19:06
  • I cannot find a detail doc for http-server-spa and it seems to be designed for static files. I think you understand the process, just translate it into its native programming language. – wannadream May 02 '17 at 19:20
0

You cannot directly get the headers from the client side.

What you can do is create a path in your application that only gets the headers from the request and pass them back to the client from the server, assuming your application has the headers on any of the pages you have.

    app.get('/getVariable', function (req, res) {
  if (req.headers && req.headers.variablename) {
    res.status(200);
    res.send({
      name: req.headers.variablename
    });
  } else {
    res.status(404);
    res.send({
      status: 404,
      message: 'Headers not found'
    })
  }
})
imGreg
  • 900
  • 9
  • 24