Наконец, я нашел решение, которое работает в Sharepoint и командах одновременно:
let loginName = this.context.pageContext.user.loginName;
let source = Sources.sharepoint;
// Obtaining loginName from Teams context
if (this.context.sdks.microsoftTeams) {
loginName = this.context.sdks.microsoftTeams.context.loginHint;
source = Sources.teams;
}
// Obtaining token provider
let tp = await this.context.aadTokenProviderFactory.getTokenProvider();
let config = tp["_defaultConfiguration"];
let aadInstanceUrl = config.aadInstanceUrl[length - 1] === "/" ? config.aadInstanceUrl : config.aadInstanceUrl + "/";
// Config context
let ctx = new AuthenticationContext({
tenant: tenantId,
clientId: clientId,
instance: aadInstanceUrl,
redirectUri: config.redirectUri,
extraQueryParameter: "login_hint=" + encodeURIComponent(loginName),
loadFrameTimeout: 60000
});
// Check user
let cu = ctx.getCachedUser();
console.log("USER", cu, loginName, ctx);
if (cu && cu.userName.toLowerCase() !== loginName.toLowerCase()) {
console.log("Clean user cache");
ctx.clearCache();
}
// Obtaining token using Adal library
let token = this.acquireToken(ctx, clientId);
// Render application
token.then((result) => {
console.log("iframe token: "+result); // "Stuff worked!"
}, (err) => {
console.log(err);
});
private acquireToken(ctx: AuthenticationContext, resource: string){
return new Promise((resolve, reject) => {
ctx.acquireToken(resource, (message, token) =>{
if(!token){
console.log("acquireToken: Error obtaining token: "+message);
reject(token);
}else{
console.log("acquireToken: Token: "+message);
resolve(token);
}
});
});
}