現在、私はこの記事に従っていますhttps://dev.outlook.com/restapi/tutorial/dotnet。すべて私はacess-tokenからユーザーの電子メールアドレスを取得できません。いくつかの記事の人々でOutlook Exchange Office 365:アクセストークンから電子メールアドレスを取得する方法は?
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;
}
"OpenIDの" 範囲内
private static string[] scopes = { "openid", "https://outlook.office.com/mail.read",
"https://outlook.office.com/calendars.read" };
を追加すると言います。しかし、それはまだ動作しません。それは成功したログインを与えることさえせず、例外をスローします。 "openid"を追加しないと、ログインに成功しますが、preferred_usernameは空です。
すぐにお返事ありがとうございます。はい、メールを送信していますが、トークンが空になっています。トークンと電子メールアドレスの両方を取得する方法。 +1。あなたのお返事ありがとうございました –
どのトークンを参照していますか? – Jackie
var authResult = authContext.AcquireTokenByAuthorizationCodeAsync( authCode、onSuccessRedirectUri、credential、scope)を待機します。 var outlookToken =新しいOutlookToken(); //トークンをセッションに保存する outlookToken.Token = authResult.Token; –