Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oauth 2.0 针对azure ad和EWS的服务身份验证_Oauth 2.0_Azure Active Directory - Fatal编程技术网

Oauth 2.0 针对azure ad和EWS的服务身份验证

Oauth 2.0 针对azure ad和EWS的服务身份验证,oauth-2.0,azure-active-directory,Oauth 2.0,Azure Active Directory,我正在构建一个守护进程/非交互式服务,以将O365中的EWS用作特定用户。为了通过OAUTH2.0与EWS通信,我正在尝试正确注册应用程序并根据AAD进行身份验证。我更希望使用基于证书的身份验证,但我没有做到这一点 代码如下(其工作原理): 此代码按预期工作,但是,我还需要确保它是安全的/符合最佳实践 在使用代码时,我将应用程序注册为“本机”,但是,我后来发现,Micrsofts手册规定,您不应在本机应用程序中使用client_secret,并且当前形式的代码也没有实际使用client_secr

我正在构建一个守护进程/非交互式服务,以将O365中的EWS用作特定用户。为了通过OAUTH2.0与EWS通信,我正在尝试正确注册应用程序并根据AAD进行身份验证。我更希望使用基于证书的身份验证,但我没有做到这一点

代码如下(其工作原理):

此代码按预期工作,但是,我还需要确保它是安全的/符合最佳实践

在使用代码时,我将应用程序注册为“本机”,但是,我后来发现,Micrsofts手册规定,您不应在本机应用程序中使用client_secret,并且当前形式的代码也没有实际使用client_secret

考虑到用例(非交互式服务作为一个特定用户进行身份验证),这是正确的方法吗

当AAD注册服务时,我应该考虑什么?

我后来发现Micrsofts手册上说不应该使用 本机应用程序的客户端加密

无法在本机应用程序(公共客户端)中使用,因为客户端机密无法可靠地存储在设备上。它是web应用程序和web API(所有机密客户端)所必需的,它们能够在服务器端安全地存储客户端机密

将用例(非交互式服务)视为一个 具体用户),这是正确的方法吗

是的,OAuth 2.0客户端凭据授予流允许web服务(机密客户端)在调用另一个web服务时使用自己的凭据(而不是模拟用户)进行身份验证。Oauth 2.0身份验证代码授权流和OpenId Connect正在模拟需要用户登录的用户

当AAD注册服务时,我应该考虑什么?


您可以阅读这篇文章,它将介绍AAD服务的主要结构。

这个答案毫无帮助。客户端凭据授予流是不可能的,因为它需要广泛的访问权限(即授予所有用户mailbokses的访问权限,而不是单个用户的访问权限)。身份验证代码授权流对于非交互式服务是不可能的,因为它不允许在没有浏览器弹出窗口(使用ADAL)的情况下登录。关于医生:我已经读了好几遍了。微软手册在将OAUTH2.0映射到ADAL和AAD应用程序注册方面做得很差。ADAL中何时使用不同的赠款流?AAD中针对这些应用程序的正确应用程序类型应该是什么?
    static void Main(string[] args)
    {
        string tenant = "redacted.onmicrosoft.com";
        string authority = "https://login.microsoftonline.com/" + tenant;
        string clientId = "44964df2-3333-2222-1111-redcated";
        string resource = "https://outlook.office365.com";
        string clientSecret = "redacted";
        string username = "username@redacted.onmicrosoft.com";
        string password = "redacted";

        AuthenticationResult authenticationResult = null;
        AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);

        var userCred = new UserPasswordCredential(username, password);
        ClientCredential clientCred = new ClientCredential(clientId, clientSecret);

        string errorMessage = null;
        try
        {
            Console.WriteLine("Trying to acquire token");
            authenticationResult = authenticationContext.AcquireTokenAsync(resource, clientId, userCred).Result;
        }
        catch (AdalException ex)
        {
            errorMessage = ex.Message;
            if (ex.InnerException != null)
            {
                errorMessage += "\nInnerException : " + ex.InnerException.Message;
            }
        }
        catch (ArgumentException ex)
        {
            errorMessage = ex.Message;
        }
        if (!string.IsNullOrEmpty(errorMessage))
        {
            Console.WriteLine("Failed: {0}" + errorMessage);
            return;
        }
        Console.WriteLine("\nMaking the protocol call\n");
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
        service.Credentials = new OAuthCredentials(authenticationResult.AccessToken);
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        service.Url = new Uri(resource + "/EWS/Exchange.asmx");
        EmailMessage email = new EmailMessage(service);
        email.ToRecipients.Add("redacted@gmail.com");
        email.Subject = "HelloWorld from EWS";
        email.Body = new MessageBody("Test from EWS");
        email.Send();
    }