2016-05-13 10 views
3

ich einen neuen Antrag auf Zugang Office-Daten durch den Rest API schreiben, deshalb würde ich das neue Authentication Model (V2.0 Endpoint)ADAL - AcquireTokenSilentAsync fehlschlägt (Azure Active Directory-Authentifizierung Bibliotheken)

What's different about the v2.0 endpoit verwenden möchte

ich ein Token mit einem Aufruf an

private static string[] scopes = { "https://outlook.office.com/mail.read", "https://outlook.office.com/calendars.read" }; 
    public async Task<ActionResult> SignIn() 
    { 
    ... SNIP 
     Uri authUri = await authContext.GetAuthorizationRequestUrlAsync(scopes, null, clientId, redirectUri, new UserIdentifier("[email protected]", UserIdentifierType.RequiredDisplayableId), null);   
     return Redirect(authUri.ToString()); 
    } 
authContext.AcquireTokenByAuthorizationCodeAsync(authCode, redirectUri, credential, scopes) 

das Problem bekommen kann, ist der zweite Aufruf von

public async Task<ActionResult> SignIn() 
    { 
    ... SNIP 
    var authResult = authContext.AcquireTokenSilentAsync(scopes, clientId, new UserIdentifier("[email protected]", UserIdentifierType.RequiredDisplayableId)) 
    } 

Das zurückgegebene Token enthält die UniqueId, aber diese Information wird nicht im Token-Objekt gespeichert. Die UserInfo des Tokens ist immer null. Da dieses Feld null ist, kann der Token-Cache das Token nicht finden.

Vielen Dank für Ihre Hinweise und Ideen

enter image description here

Token Retour

{ 
    "aud":"https://outlook.office.com", 
    "iss":"https://sts.windows.net/f2ac6f3f-3df0-4068-a677-e4dfdf924b2/", 
    "iat":146 dfdf21, 
    "nbf":146 dfdf4621, 
    "exp":1463 dfdf38521, 
    "acr":"1", 
    "amr":[ 
     "pwd" 
    ], 
    "appid":"b13dfdf9-0561-4dfdff5-945c-778dfdf0de5cd", 
    "appidacr":"1", 
    "family_name":"Pan", 
    "given_name":"Peter", 
    "ipaddr":"12.12.12.17", 
    "name":"Peter Pan", 
    "oid":"4b83dfdfdb-f6db-433e-b70a-2f9a6dbbeb48", 
    "puid":"100dfdfdfF5FBC", 
    "scp":"Calendars.Read Mail.Read Mail.ReadWrite", 
    "sub":"Z-chdfdsfnWqduUkCGZpsIdp-fdhpMMqqtwcHGs", 
    "tid":"f2ac6f3f-3560-4068-a677-e4bfe0c924b2", 
    "unique_name":"[email protected]", 
    "upn":"[email protected]", 
    "ver":"1.0" 
} 

ähnliche Frage: Here

Antwort

3

Microsoft die profile_info entfernt hat, wie Sie hier lesen : Important Updates to ADV2

Im Moment hat die Bibliothek einen Fehler, weil sie es immer noch überprüft, und wenn es null ist, wird es die Benutzerinformationen nicht zurückgeben.

Die richtigen Informationen sind in token_id ...

Klasse: TokenResponse

private AuthenticationResultEx GetResult(string token, string scope, long expiresIn) 
{ 
    DateTimeOffset expiresOn = (DateTimeOffset) (DateTime.UtcNow + TimeSpan.FromSeconds((double) expiresIn)); 
    AuthenticationResult authenticationResult = new AuthenticationResult(this.TokenType, token, expiresOn); 
    ProfileInfo profileInfo = ProfileInfo.Parse(this.ProfileInfoString); 
    if (profileInfo != null) 
    { 
    string tenantId = profileInfo.TenantId; 
    string str1 = (string) null; 
    string str2 = (string) null; 
    if (!string.IsNullOrWhiteSpace(profileInfo.Subject)) 
     str1 = profileInfo.Subject; 
    if (!string.IsNullOrWhiteSpace(profileInfo.PreferredUsername)) 
     str2 = profileInfo.PreferredUsername; 
    authenticationResult.UpdateTenantAndUserInfo(tenantId, this.ProfileInfoString, new UserInfo() 
    { 
     UniqueId = str1, 
     DisplayableId = str2, 
     Name = profileInfo.Name, 
     Version = profileInfo.Version 
    }); 
    } 
    return new AuthenticationResultEx() 
    { 
    Result = authenticationResult, 
    RefreshToken = this.RefreshToken, 
    ScopeInResponse = AdalStringHelper.CreateArrayFromSingleString(scope) 
    }; 
} 

ich sie hoffen, es bald zu beheben, warte ich :-) auch

Edit:

Ich fand hier etwas interessantes: Dev Outlook get started

Wie ich bereits sagte, sind alle in token_id gespeicherten Informationen, über die Sie in den Link lesen:

Die Vorabversion von ADAL v4 ist die ID-Token nicht direkt zurückgeben, aber es ist zugänglich . Die hier enthaltene Methode soll dieses Problem umgehen, bis ADAL aktualisiert wird.

Sie erklären, eine Möglichkeit, den Token zuzugreifen:

private string GetUserEmail(AuthenticationContext context, string clientId) 
{ 
    // ADAL caches the ID token in its token cache by the client ID 
    foreach (TokenCacheItem item in context.TokenCache.ReadItems()) 
    { 
     if (item.Scope.Contains(clientId)) 
     { 
      return GetEmailFromIdToken(item.Token); 
     } 
    } 
    return string.Empty; 
} 

    private string GetEmailFromIdToken(string token) 
{ 
    // JWT is made of three parts, separated by a '.' 
    // First part is the header 
    // Second part is the token 
    // Third part is the signature 
    string[] tokenParts = token.Split('.'); 
    if (tokenParts.Length < 3) 
    { 
     // Invalid token, return empty 
    } 
    // Token content is in the second part, in urlsafe base64 
    string encodedToken = tokenParts[1]; 
    // Convert from urlsafe and add padding if needed 
    int leftovers = encodedToken.Length % 4; 
    if (leftovers == 2) 
    { 
     encodedToken += "=="; 
    } 
    else if (leftovers == 3) 
    { 
     encodedToken += "="; 
    } 
    encodedToken = encodedToken.Replace('-', '+').Replace('_', '/'); 
    // Decode the string 
    var base64EncodedBytes = System.Convert.FromBase64String(encodedToken); 
    string decodedToken = System.Text.Encoding.UTF8.GetString(base64EncodedBytes); 
    // Load the decoded JSON into a dynamic object 
    dynamic jwt = Newtonsoft.Json.JsonConvert.DeserializeObject(decodedToken); 
    // User's email is in the preferred_username field 
    return jwt.preferred_username; 
} 

ich nicht dieses noch nicht getestet, aber ich werde diesen Beitrag aktualisieren, wenn ich es getestet haben, oder ein anderer wird bitte machen ein Kommentar, wenn er schneller ist :-)

+1

Im Moment benutze ich diese Problemumgehung bereits, aber auf diese Weise kann der integrierte Token-Cache der ADAL-Bibliothek nicht verwendet werden –