적용 대상:
인력 테넌트(자세한 정보)
공용 클라이언트 애플리케이션의 인스턴스를 빌드한 후에는 이 인스턴스를 사용하여 토큰을 획득한 다음 웹 API를 호출하는 데 사용합니다.
권장 패턴
웹 API는 범위에 의해 정의됩니다. 애플리케이션을 통해 제공하려는 경험이 무엇이든, 다음과 같은 패턴을 사용합니다.
-
AcquireTokenSilent
를 호출하여 토큰 캐시에서 시스템적으로 토큰 가져오기를 시도합니다.
- 이 호출이 실패하면 사용하려는
AcquireToken
흐름을 사용합니다. 여기서는 이 흐름이 AcquireTokenXX
로 표현됩니다.
MSAL.NET에서
AuthenticationResult result;
var accounts = await app.GetAccountsAsync();
IAccount account = ChooseAccount(accounts); // for instance accounts.FirstOrDefault
// if the app manages is at most one account
try
{
result = await app.AcquireTokenSilent(scopes, account)
.ExecuteAsync();
}
catch(MsalUiRequiredException ex)
{
result = await app.AcquireTokenXX(scopes, account)
.WithOptionalParameterXXX(parameter)
.ExecuteAsync();
}
Set<IAccount> accountsInCache = pca.getAccounts().join();
// Take first account in the cache. In a production application, you would filter
// accountsInCache to get the right account for the user authenticating.
IAccount account = accountsInCache.iterator().next();
IAuthenticationResult result;
try {
SilentParameters silentParameters =
SilentParameters
.builder(SCOPE, account)
.build();
// try to acquire token silently. This call will fail since the token cache
// does not have any data for the user you are trying to acquire a token for
result = pca.acquireTokenSilently(silentParameters).join();
} catch (Exception ex) {
if (ex.getCause() instanceof MsalException) {
InteractiveRequestParameters parameters = InteractiveRequestParameters
.builder(new URI("http://localhost"))
.scopes(SCOPE)
.build();
// Try to acquire a token interactively with system browser. If successful, you should see
// the token and account information printed out to console
result = pca.acquireToken(parameters).join();
} else {
// Handle other exceptions accordingly
throw ex;
}
}
return result;
iOS 및 macOS용 MSAL에서
Objective-C:
MSALAccount *account = [application accountForIdentifier:accountIdentifier error:nil];
MSALSilentTokenParameters *silentParams = [[MSALSilentTokenParameters alloc] initWithScopes:scopes account:account];
[application acquireTokenSilentWithParameters:silentParams completionBlock:^(MSALResult *result, NSError *error) {
// Check the error
if (error && [error.___domain isEqual:MSALErrorDomain] && error.code == MSALErrorInteractionRequired)
{
// Interactive auth will be required, call acquireTokenWithParameters:error:
}
}];
재빠르다:
guard let account = try? application.account(forIdentifier: accountIdentifier) else { return }
let silentParameters = MSALSilentTokenParameters(scopes: scopes, account: account)
application.acquireTokenSilent(with: silentParameters) { (result, error) in
guard let authResult = result, error == nil else {
let nsError = error! as NSError
if (nsError.___domain == MSALErrorDomain &&
nsError.code == MSALError.interactionRequired.rawValue) {
// Interactive auth will be required, call acquireToken()
return
}
return
}
}
MSAL Node에서 PKCE(Proof Key for Code Exchange)를 사용하여 인증 코드 흐름을 통해 토큰을 획득합니다. MSAL Node는 메모리 내 토큰 캐시를 사용하여 캐시에 사용자 계정이 있는지 확인합니다. 계정이 있는 경우에는 계정 개체를 acquireTokenSilent()
메서드에 전달하여 캐시된 액세스 토큰을 검색할 수 있습니다.
const msal = require("@azure/msal-node");
const msalConfig = {
auth: {
clientId: "your_client_id_here",
authority: "your_authority_here",
}
};
const pca = new msal.PublicClientApplication(msalConfig);
const msalTokenCache = pca.getTokenCache();
let accounts = await msalTokenCache.getAllAccounts();
if (accounts.length > 0) {
const silentRequest = {
account: accounts[0], // Index must match the account that is trying to acquire token silently
scopes: ["user.read"],
};
pca.acquireTokenSilent(silentRequest).then((response) => {
console.log("\nSuccessful silent token acquisition");
console.log("\nResponse: \n:", response);
res.sendStatus(200);
}).catch((error) => console.log(error));
} else {
const {verifier, challenge} = await msal.cryptoProvider.generatePkceCodes();
const authCodeUrlParameters = {
scopes: ["User.Read"],
redirectUri: "your_redirect_uri",
codeChallenge: challenge, // PKCE Code Challenge
codeChallengeMethod: "S256" // PKCE Code Challenge Method
};
// get url to sign user in and consent to scopes needed for application
pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {
console.log(response);
const tokenRequest = {
code: response["authorization_code"],
codeVerifier: verifier, // PKCE Code Verifier
redirectUri: "your_redirect_uri",
scopes: ["User.Read"],
};
// acquire a token by exchanging the code
pca.acquireTokenByCode(tokenRequest).then((response) => {
console.log("\nResponse: \n:", response);
}).catch((error) => {
console.log(error);
});
}).catch((error) => console.log(JSON.stringify(error)));
}
result = None
# Firstly, check the cache to see if this end user has signed in before
accounts = app.get_accounts(username=config["username"])
if accounts:
result = app.acquire_token_silent(config["scope"], account=accounts[0])
if not result:
result = app.acquire_token_by_xxx(scopes=config["scope"])
데스크톱 애플리케이션에서 토큰을 획득할 수 있는 다양한 방법이 있습니다.
사용자가 MFA(Multi-Factor Authentication)를 사용하여 애플리케이션에 로그인해야 하는 경우, 사용자가 차단됩니다.
다음 단계
이 시나리오의 다음 문서로 이동하여 데스크톱 앱에서 웹 API를 호출합니다.