代理提供程序Proxy provider

使用代理提供程序时,可以使用后端身份验证(如代表身份验证2.0)为 Microsoft Graph 工具包供电,方法是通过将所有呼叫通过您自己的后端路由到 Microsoft graph 工具包。When you use the proxy provider, you can use your backend authentication (such as Auth2.0 On-Behalf-Of flow) to power the Microsoft Graph Toolkit by routing all calls to Microsoft Graph through your own backend.

后端服务必须公开将为每个 Microsoft Graph 调用调用的 API。Your backend service must expose an API that will be called for every call to Microsoft Graph. 例如,当组件尝试获取资源时,ProxyProvider 将改为调用基本 API 并追加该资源。For example, when a component attempts to get a resource, the ProxyProvider will instead call your base API and append that resource.

https://graph.microsoft.com/v1.0/me => https://myurl.com/api/GraphProxy/v1.0/me

然后,您的 API 实现应代表用户调用 Microsoft Graph 并将结果返回到组件。Your API implementation should then call Microsoft Graph on behalf of the user and return the results to the component.

有关实现示例,请参阅ASP.NET MVC 示例For an implementation example, see the ASP.NET MVC sample.

若要了解有关身份验证提供程序的详细信息,请参阅提供程序To learn more about authentication providers, see providers.

入门Get started

您可以用 HTML 或 JavaScript 初始化代理提供程序。You can initialize the proxy provider in HTML or JavaScript. 每页只应执行一次此操作。You should do this only once per page.

在 HTML 页面中初始化Initialize in your HTML page

在 HTML 中初始化代理提供程序是定义自定义服务器端身份验证的您自己的路由的最简单方法。Initializing the proxy provider in HTML is the simplest way to define your own route for custom server-side authentication. 使用mgt-proxy-provider组件设置graph-代理 urlUse the mgt-proxy-provider component to set the graph-proxy-url. 这会将定义的代理提供程序设置为全局提供程序。This will set the defined proxy provider as the global provider.

<mgt-proxy-provider graph-proxy-url="https://myurl.com/api/GraphProxy"></mgt-proxy-provider>
属性Attribute 说明Description
graph-代理-urlgraph-proxy-url 代理 API 的基 URL。Base URL for the proxy API.

在 JavaScript 中初始化Initialize in JavaScript

您可以通过在 JavaScript 中初始化提供程序来提供更多选项。You can provide more options by initializing the provider in JavaScript.

import {Providers, ProxyProvider} from '@microsoft/mgt'

Providers.globalProvider = new ProxyProvider("https://myurl.com/api/GraphProxy");

(可选)通过将可选函数用作构造函数中的第二个参数,可以向代理 api 发送包含每个请求的其他标头。Optionally, you can send additional headers with each request to your proxy api by using an optional function as the second parameter in the constructor.

import {Providers, ProxyProvider} from '@microsoft/mgt'

Providers.globalProvider = new ProxyProvider("https://myurl.com/api/GraphProxy", async () => {
  return {
    header: 'value',
    header2: 'value2'
  };
);

当您需要将令牌或其他标头传递给后端时,这将非常有用This is useful when you need to pass tokens or other headers to your backend

如果要使用mgt-login组件,还应为提供程序指定loginlogout函数:If you will be using the mgt-login component, you should also specify the login and logout functions for the provider:

import {Providers, ProxyProvider} from '@microsoft/mgt'

let provider = new ProxyProvider("https://myurl.com/api/GraphProxy");
provider.login = () => { /* will be called when "Sign In" is clicked */ };
provider.logout = () => { /* will be called when "Sign Out" is called */ };

Providers.globalProvider = provider;