Azure ad b2c 使用msal连接到Azure B2C-状态参数

Azure ad b2c 使用msal连接到Azure B2C-状态参数,azure-ad-b2c,msal,Azure Ad B2c,Msal,我正在使用示例from:作为实现B2C注册的基础 如何在示例中传递state参数?我看到有一个关于状态的问题,所以我想在示例中可以使用状态。但是,我不知道如何使用它以及在返回令牌后如何检索它。在查看中,我看不到如何控制状态的值AuthenticationRequestParameters,并且将state的值设置为新guid 示例: 在MSAL.js的以下代码中,我们无法控制authenticationRequest变量 loginDirect(作用域?:数组,extraQueryParame

我正在使用示例from:作为实现B2C注册的基础

如何在示例中传递state参数?我看到有一个关于状态的问题,所以我想在示例中可以使用状态。但是,我不知道如何使用它以及在返回令牌后如何检索它。

在查看中,我看不到如何控制
状态的值<当启用
AuthenticationRequestParameters
时,不会公开code>AuthenticationRequestParameters
,并且将
state
的值设置为新guid


示例

在MSAL.js的以下代码中,我们无法控制
authenticationRequest
变量

loginDirect(作用域?:数组,extraQueryParameters?:字符串):无效{
...
this.authorityInstance.ResolveEndpointsAsync()
.然后(()=>{
const authenticationRequest=新的AuthenticationRequestParameters(this.authorityInstance、this.clientId、scopes、ResponseTypes.id\u标记、this.\u重定向URI);
...
});
...
}

您可以在loginRequest上发送
状态
参数:

const loginRequest = {
   ...
    scopes: "your scopes",
    state: "my custom state value"
}
然后在
accountState
上的响应中捕获它,如下所示:

clientApplication.loginPopup(loginRequest).then(function (loginResponse) {
    if (loginResponse.account) {
        // will print: my custom state value
        console.log(loginResponse.accountState);
        ....
    }
});

您可以在这里找到文档:

我在loginDirect()方法中使用状态。。因此,我将在这里发布我的代码,这应该足以帮助您实现这一点。我在angular中使用MSAL,但是我调用的方法应该是相同的

在此示例中,用户单击一个登录按钮,该按钮调用一个登录方法:

{
   const args: AuthenticationParameters = {
     state: "some string" //set state parameter (type: string)
   };
   this.msalService.loginRedirect(args);
}
然后,此代码将用户重定向到登录。。然后返回到您的网站(您的重定向URI)。。在此页面上,您应该实现
handleRedirectCallback
方法,该方法将在重定向用户后触发。在这个回调中,您还将从登录过程中得到一个响应(或错误),其中包括您的状态字符串

this.msalService.handleRedirectCallback((authError, response) => {
  if (response) {
     const state = this.msalService.getAccountState(response.accountState);
     // you don't need to use "this.msalService.getAccountState()" I think
     ...
     do something with state. I use it to redirect back to initial page url. 
     ...
  }
});

你能提供一个“我看到有一个关于国家的问题”的链接吗?我很好奇。如果库正在创建状态参数,将其公开给用户有什么好处?请参阅关于使用状态参数控制重定向@用户911