Azure active directory oauth 2.0是否有永久同意的方法?

Azure active directory oauth 2.0是否有永久同意的方法?,azure-active-directory,microsoft-graph-api,Azure Active Directory,Microsoft Graph Api,我正在尝试创建一个使用MicrosoftGraph和OAuth2.0身份验证的应用程序。每当我开始申请时,我必须给予同意 是否有任何方法可以永久地给予应用程序用户许可?因此,您有一个如下定义的类: //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. // All rights reserved. //

我正在尝试创建一个使用MicrosoftGraph和OAuth2.0身份验证的应用程序。每当我开始申请时,我必须给予同意


是否有任何方法可以永久地给予应用程序用户许可?

因此,您有一个如下定义的类:

//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//------------------------------------------------------------------------------

using System.IO;
using System.Security.Cryptography;
using Microsoft.Identity.Client;

namespace OutlookCalIFConsole
{
    static class TokenCacheHelper
    {

        /// <summary>
        /// Get the user token cache
        /// </summary>
        /// <returns></returns>
        public static TokenCache GetUserCache()
        {
            if (usertokenCache == null)
            {
                usertokenCache = new TokenCache();
                usertokenCache.SetBeforeAccess(BeforeAccessNotification);
                usertokenCache.SetAfterAccess(AfterAccessNotification);
            }
            return usertokenCache;
        }

        static TokenCache usertokenCache;

        /// <summary>
        /// Path to the token cache
        /// </summary>
        public static string CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + "msalcache.txt";

        private static readonly object FileLock = new object();

        public static void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            lock (FileLock)
            {
                args.TokenCache.Deserialize(File.Exists(CacheFilePath)
                    ? File.ReadAllBytes(CacheFilePath)
                    : null);
            }
        }

        public static void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            // if the access operation resulted in a cache update
            if (args.TokenCache.HasStateChanged)
            {
                lock (FileLock)
                {
                    // reflect changesgs in the persistent store
                    File.WriteAllBytes(CacheFilePath, args.TokenCache.Serialize());
                    // once the write operationtakes place restore the HasStateChanged bit to filse
                    args.TokenCache.HasStateChanged = false;
                }
            }
        }
    }
}
然后,您应该能够使用您的正常凭证代码。以上课程取自此处:

还有一个关于力学的完整讨论(参见讨论主题)

比如:

TokenCacheHelper.CacheFilePath = Program.Options.TokenCachePath;
PublicClientApp = new PublicClientApplication(_AppID, "https://login.microsoftonline.com/common", TokenCacheHelper.GetUserCache());
   public bool InitAuthenticatedClientAsync()
    {
        string appID = "~~~";
        PublicClientApplication PublicClientApp = new PublicClientApplication(appID, "https://login.microsoftonline.com/common", TokenCacheHelper.GetUserCache());
        string[] _scopes = new string[] { "user.read", "calendars.readwrite" };
        bool bSuccess = true;

        _graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    try
                    {
                        _AuthResult = await PublicClientApp.AcquireTokenSilentAsync(_scopes, PublicClientApp.Users.FirstOrDefault());
                    }
                    catch (MsalUiRequiredException ex)
                    {
                        // A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
                        System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

                        try
                        {
                            _AuthResult = await PublicClientApp.AcquireTokenAsync(_scopes);
                        }
                        catch (MsalException msalex)
                        {
                            SimpleLog.Log(msalex);
                            Console.WriteLine("GetAuthenticatedClientAsync: Acquire token error. See log.");
                            bSuccess = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        SimpleLog.Log(ex);
                        Console.WriteLine("GetAuthenticatedClientAsync: Acquire token silently error. See log.");
                        bSuccess = false;
                    }

                    if(bSuccess)
                    {
                        // Append the access token to the request.
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", _AuthResult.AccessToken);

                        // Get event times in the current time zone.
                        requestMessage.Headers.Add("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\"");
                    }
                }));

        return bSuccess;
    }

希望这能有所帮助。

正如其他人所指出的,这里有两个选项:

  • 如果您请求范围
    脱机访问
    ,您将收到
    刷新令牌
    以及
    访问令牌
    。当您的
    access\u令牌
    过期时,可以将
    refresh\u令牌
    提交到
    /token
    端点以获得新的集合

    我写了一篇关于v2端点的入门文章,其中介绍了如何使用它,您可能会发现它对此很有帮助

  • 使用
    client\u凭证
    OAuth授权。此授权专门用于服务到服务/守护程序场景。它们的工作方式稍有不同,因为它们使用应用程序作用域而不是委托,并且上下文中没有用户(因此对
    /me
    的调用需要替换为
    /users/{id}

    客户端凭据也始终需要管理员同意。由于没有特定的用户进行身份验证,因此需要管理员预先授权您的应用程序。一旦发生这种情况,您的应用程序将保持授权(除非管理员明确阻止)

    您可以找到有关此过程在以下情况下如何工作的详细信息:


  • 对您可以编写一个令牌缓存帮助器类,从中进行读取和写入。如果没有人帮助查找,我将稍后查找我的代码。他们也有关于它的文章。但是我需要在某个时候更新缓存吗?我的最终目标是将应用程序用作后台服务。是的。您可以访问和刷新令牌。刷新令牌持续很长时间,用于获取新的访问令牌。我假设这需要用户交互,类似于首先同意?不。它是缓存。您存储同意屏幕的结果。就是这样。刷新令牌可能会因各种原因而过期,所以您不应该期望它总是有效。在这种情况下,您需要用户重新验证。非常好的一点,我应该明确提到这一点。这是我建议使用客户端凭据的原因之一。除非你的应用偶尔以交互方式使用,否则你几乎肯定会遇到服务失败,而没有明确的方法来解决问题。不过,客户端凭据的一个不好的方面是组织范围的访问。我们在其中一个应用程序中使用委托访问+刷新令牌的方法,因此我们对客户租户的权限要求最低。这通常归结为服务的关键任务。如果这是一个可以持续一段时间而不会引起火灾的东西,那么我通常建议刷新令牌,如果需要重新授权的话,让服务向管理员发送电子邮件。对于任务关键型服务,最好通过客户端凭据过程,避免潜在的停机时间。