我们的SPFx Web部件运行良好,但即使用户在访问包含SPFx Web部件的页面时已登录到Office 365,getCachedUser对象也始终为null。这会强制重定向登录,这会立即自动重定向回我们的应用程序页面。
我们使用直接JavaScript的原始PoC从未这样做过,但SPFx Web部件总是在第一次点击时重定向。
有没有人见过并修复过这种行为?我们应该在Authentication包装器中寻找什么?
以下是我们代码的示例:
import * as AuthenticationContext from 'adal-angular';
import '../WebPartAuthenticationContext';
constructor(props) {
super(props);
this.state = {
detailModal: false,
item: null,
columns: [],
rows: [],
titleFilter: null,
filterBy: 'givenName'
};
const config: IAdalConfig = {
tenant: this.props.tenant,
clientId: this.props.clientId,
postLogoutRedirectUri: window.location.origin
};
config.webPartId = '<GUID>';
config.popUp = false;
config.callback = (error: any, token: string): void => {};
this.authCtx = new AuthenticationContext(config);
this.authCtx.handleWindowCallback();
AuthenticationContext.prototype._singletonInstance = undefined;
}
private getRealItemsByAzureGraph(searchFor: string){
var params = {
'api-version': '1.6',
'$filter': searchFor,
'$top' : '999'
};
var baseURL = 'https://graph.windows.net/myorganization/users?' + jquery.param(params);
var user = this.authCtx.getCachedUser();
if (user) {
this.authCtx.acquireToken("https://graph.windows.net", (error, accessToken) => {
if (error || !accessToken) { console.log(error); }
else {
jquery.ajax({
type: 'GET',
url: baseURL,
dataType: "json",
headers: {
'Authorization': 'Bearer ' + accessToken,
'Accept': 'application/json'
}
}).done((data) => {
var res = [];
data.value.forEach((itm) => {
if (itm.surname && itm.mail && itm.givenName && this.notInKeywords(itm)){
if (this.props.showGuestUsers) {
res.push(itm);
} else {
if (itm.userType !== "Guest") {
res.push(itm);
}
}
}
});
this.setState(
{
rows: res,
}
);
}).fail((err) => {
console.log(err.responseText);
});
}
});
} else {
this.authCtx.login();
}
}
此代码(以及其他支持代码)运行良好,即使用户已登录,我们也无法绕过重定向.getCachedUser方法始终为null / undefined。