简体   繁体   English

如何通过SSO将标头变量传递给我的angular 2应用程序?

[英]How to get header variables passed to my angular 2 application via SSO?

I am new to Angular 2 + NodeJS. 我是Angular 2 + NodeJS的新手。 I come from .NET. 我来自.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. 在.NET中,我知道您将只获取请求对象并将服务器变量拉出,但是在angular 2中,您似乎必须设法获取请求并将标头变量拉出。

My company has its own SSO, I need to get the header variables it adds before coming to my application. 我公司有自己的SSO,在进入我的应用程序之前,我需要获取它添加的标头变量。

How do I get the header variables that are passed to my application? 如何获取传递到应用程序的标头变量? Is there a standard way to get it in Angular 2? 在Angular 2中有获取它的标准方法吗?

When I was trying to research it myself all I kept getting a lot of "set header" situations 当我自己尝试研究时,我总是遇到很多“设置标头”的情况

I am thinking of an alternative solution for you. 我正在为您寻找替代解决方案。 See my graph. 看我的图。 在此处输入图片说明

Since you mentioned .NET, use MVC here. 既然您提到了.NET,请在此处使用MVC。 https://www.asp.net/mvc In controller, you can do something like this: https://www.asp.net/mvc在控制器中,您可以执行以下操作:

    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: 在您的SsoMiddleware.cshtml视图中,添加Javascript:

    @model string

    ...

    <script type="text/javascript">
        localStorage.setItem('auth-string', '@(Model)');
        window.location.href = 'angular-app.html';
    </script>

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'
    })
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM