我正在尝试两个方案:1.重定向可以从外部站点到我站点的一个页面之一。我正在尝试使用Document.Referrer捕获外部站点名称,但它是空的。[注意 - 我在Azure AD身份验证后重定向。]
- 在我的网站内(身份验证 - 需要清楚)。我从一个页面重定向到另一页。在下一页上,我试图通过Document.Referrer获取上一页URL。但是在这里,它是空白的。
请建议如何使用Angular 2 Typescript实现上一页从重定向到当前页面的位置。
您可以尝试将转介器放在state
变量中,然后在Azuread重定向后读取。
有关状态变量和您可以发送的其他参数的一些信息,请查看此文档。
Microsoft还提供了代码示例Angular2-connect-est-sample来调用Angular2中的Microsoft Graph。
在此代码示例中,我们需要先用Azure AD进行身份验证。因此,我们可以在Angular2中引用有关身份验证的代码。
但是,当我尝试登录时,我遇到了问题,例如重定向URL与不匹配。更改下面的代码后,代码示例对我来说很好。
authhelper.ts
import { Injectable } from "angular2/core";
import { SvcConsts } from "../svcConsts/svcConsts";
@Injectable()
export class AuthHelper {
//function to parse the url query string
private parseQueryString = function(url) {
var params = {}, queryString = url.substring(1),
regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(queryString)) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return params;
}
private params = this.parseQueryString(location.hash);
public access_token:string = null;
constructor() {
//check for id_token or access_token in url
if (this.params["id_token"] != null)
this.getAccessToken();
else if (this.params["access_token"] != null)
this.access_token = this.params["access_token"];
}
// add this function to modify the redirect url using the base address
getRedrectUrl(){
return window.location.href.substr(0,window.location.href.indexOf("#"));
}
login() {
//redirect to get id_token
window.location.href = "https://login.microsoftonline.com/" + SvcConsts.TENANT_ID +
"/oauth2/authorize?response_type=id_token&client_id=" + SvcConsts.CLIENT_ID +
"&redirect_uri=" + encodeURIComponent(this.getRedrectUrl()) +
"&state=SomeState&nonce=SomeNonce";
}
private getAccessToken() {
//redirect to get access_token
window.location.href = "https://login.microsoftonline.com/" + SvcConsts.TENANT_ID +
"/oauth2/authorize?response_type=token&client_id=" + SvcConsts.CLIENT_ID +
"&resource=" + SvcConsts.GRAPH_RESOURCE +
"&redirect_uri=" + encodeURIComponent(this.getRedrectUrl()) +
"&prompt=none&state=SomeState&nonce=SomeNonce";
}
}
请告诉我是否有帮助。