Microsoft Graph SDK - 登录 [英] Microsoft Graph SDK - Login

查看:35
本文介绍了Microsoft Graph SDK - 登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 MS Graph 仅登录一次?目前,每当我调用 graphServiceClient 时,它都会要求我登录或选择登录用户.有什么办法可以避免选择登录用户的过程?提前致谢.

Is it possible to only log in once using MS Graph? Currently whenever I call the graphServiceClient it will ask me to sign in or to choose the signed in user. Is there any way that the process of choosing the signed in user can be avoided? Thanks in advance.

目前这是我初始化 graphService 的方式.

Currently this is how I initialize the graphService.

    public static GraphServiceClient GetAuthenticatedClient()
    {
        GraphServiceClient graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    string appID = ConfigurationManager.AppSettings["ida:AppId"];

                    PublicClientApplication PublicClientApp = new PublicClientApplication(appID);
                    string[] _scopes = new string[] { "Calendars.read", "Calendars.readwrite" };

                    AuthenticationResult authResult = null;
                    authResult = await PublicClientApp.AcquireTokenAsync(_scopes); //Opens Microsoft Login Screen    

                    // Append the access token to the request.
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
                }));
        return graphClient;
    }

现在,每当我使用标准 SDK 功能时,它都会要求我登录:

等待 graphClient.Me.Events[testID].Request().UpdateAsync(x);

然后当我使用其他功能时它会再次询问我:

等待 graphClient.Me.Events.Request().AddAsync(newEvent);

推荐答案

是的,这是可能的.AcquireTokenAsync 总是触发登录.相反,您需要实现令牌缓存并使用 AcquireTokenSilentAsync.https://docs.microsoft.com/en-us/outlook/rest/dotnet-tutorial 有一个 web 应用示例.

Yes it is possible. AcquireTokenAsync does always trigger logon. Instead, you need to implement a token cache and use AcquireTokenSilentAsync. https://docs.microsoft.com/en-us/outlook/rest/dotnet-tutorial has a web app example.

这篇关于Microsoft Graph SDK - 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆