“意外错误"使用Connect-MsolService -AccessToken连接到Azure AD [英] "Unexpected Error" connecting to Azure AD with Connect-MsolService -AccessToken

查看:107
本文介绍了“意外错误"使用Connect-MsolService -AccessToken连接到Azure AD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure AD PS模块的新预览版.我正在尝试通过新的AccessToken参数进行连接:

I'm using the new preview version of the Azure AD PS module. I'm trying to connect via the new AccessToken parameter:

Connect-MsolService - AccessToken ey...

但是我又收到了意外错误".

But I'm getting an "Unexpected Error" back.

Connect-MsolService : An unexpected error occurred.
At line:1 char:1
+ Connect-MsolService -AccessToken eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.InvalidHeaderException,Microsoft.Online.Admin
istration.Automation.ConnectMsolService

Connect-MsolService : Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was
thrown.
At line:1 char:1
+ Connect-MsolService -AccessToken eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ik1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.MicrosoftOnlineException,Microsoft.Online.Adm
inistration.Automation.ConnectMsolService

我知道我使用的访问令牌很好,因为我可以使用它来从Postman调用Graph API.有没有人得到这个工作?

I know the access token I'm using is good because I can use it to call the Graph API from Postman. Has anyone gotten this to work?

不知道投票否决的原因,只是为了表明我做了我的作业,这是PS模块在后台进行的请求/响应,由Fiddler跟踪捕获.它包含有用的消息用户身份标头无效".

Not sure of the reason for the downvote, but just to show I did my homework, here's the request/response that the PS module is making behind the scenes, captured with a Fiddler trace. It contains the helpful message "The user identity header is invalid."

请求

POST https://provisioningapi.microsoftonline.com/provisioningwebservice.svc HTTP/1.1

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://provisioning.microsoftonline.com/IProvisioningWebService/MsolConnect</a:Action>
    <a:MessageID>urn:uuid:df0e35bd-ef05-48cd-a623-a1134b0b2ed6</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
    <UserIdentityHeader xmlns="http://provisioning.microsoftonline.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <BearerToken xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration.WebService">Bearer ey...</BearerToken>
      <LiveToken i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration.WebService"/>
    </UserIdentityHeader>
    <ClientVersionHeader xmlns="http://provisioning.microsoftonline.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <ClientId xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration.WebService">50afce61-c917-435b-8c6d-60aa5a8b8aa7</ClientId>
      <Version xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration.WebService">1.1.8806.11</Version>
    </ClientVersionHeader>
    <ContractVersionHeader xmlns="http://becwebservice.microsoftonline.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <BecVersion xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration.WebService">Version32</BecVersion>
    </ContractVersionHeader>
    <TrackingHeader xmlns="http://becwebservice.microsoftonline.com/">bf71f0c6-add7-4046-9209-bfd584ca3c28</TrackingHeader>
    <a:To s:mustUnderstand="1">https://provisioningapi.microsoftonline.com/provisioningwebservice.svc</a:To>
  </s:Header>
  <s:Body>
    <MsolConnect xmlns="http://provisioning.microsoftonline.com/">
      <request xmlns:b="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration.WebService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <b:BecVersion>Version4</b:BecVersion>
        <b:TenantId i:nil="true"/>
        <b:VerifiedDomain i:nil="true"/>
      </request>
    </MsolConnect>
  </s:Body>
</s:Envelope>

回复

HTTP/1.1 500 Internal Server Error

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://provisioning.microsoftonline.com/IProvisioningWebService/MsolConnectInvalidHeaderExceptionFault</a:Action>
    <a:RelatesTo>urn:uuid:df0e35bd-ef05-48cd-a623-a1134b0b2ed6</a:RelatesTo>
  </s:Header>
  <s:Body>
    <s:Fault>
      <s:Code>
        <s:Value>s:Sender</s:Value>
      </s:Code>
      <s:Reason>
        <s:Text xml:lang="en-US">The creator of this fault did not specify a Reason.</s:Text>
      </s:Reason>
      <s:Detail>
        <InvalidHeaderException xmlns="http://schemas.datacontract.org/2004/07/Microsoft.Online.Administration.WebService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <HelpLink i:nil="true"/>
          <Message>The user identity header is invalid.</Message>
          <OperationId i:nil="true"/>
          <Source>Microsoft.Online.Administration.PublicBecWebService</Source>
          <StackTrace>   at Microsoft.Online.Administration.WebService.BecWebServiceAuthenticationManager.ValidateJwtTokenV2(String bearerToken) in x:\bt\533229\repo\src\dev\om\administration\publicbecwebservice\BecWebServiceAuthenticationManager.cs:line 371&#xD;
    at Microsoft.Online.Administration.WebService.BecWebServiceAuthenticationManager.CheckAccessCore(OperationContext operationContext) in x:\bt\533229\repo\src\dev\om\administration\publicbecwebservice\BecWebServiceAuthenticationManager.cs:line 723</StackTrace>
        </InvalidHeaderException>
      </s:Detail>
    </s:Fault>
  </s:Body>
</s:Envelope>

这是我获取访问令牌的方式.我使用的是客户端凭据流,因为最终我的目标是从Azure自动化运行此凭据.

Here's how I'm obtaining the access token. I'm using the client credentials flow since ultimately my goal is to run this from Azure Automation.

$clientId = "20bc779d-0edb-4a00-becf-xxx"
$redirectUri = new-object System.Uri("urn:ietf:wg:oauth:2.0:oob")
$resourceId = "https://graph.windows.net"
$authority = "https://login.windows.net/mydirectory.onmicrosoft.com"
$key = ConvertTo-SecureString $keyFromAzurePortal -AsPlainText -Force
$cred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential ($clientId, $key)

$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext($authority)
$authResult = $authContext.AcquireToken($resourceId, $cred)

推荐答案

我无法复制您的问题.让我告诉你我为使其正常工作做了什么:

I was not able to replicate your issue. Let me tell you what I did to get it working:

我首先获得了针对Graph API的本机客户端应用程序的访问令牌:

I first got an access token for a native client app targeting the Graph API:

$clientId = "<GUID>";
$tenantId = "<tenant>.onmicrosoft.com";
$resourceId = "https://graph.windows.net"
$redirectUri = new-object System.Uri("urn:ietf:wg:oauth:2.0:oob")
$login = "https://login.microsoftonline.com"

$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext("{0}/{1}" -f $login,$tenantId);
$authenticationResult = $authContext.AcquireToken($resourceId,$clientID,$redirectUri);
$token = $authenticationResult.AccessToken

然后我在PowerShell中使用了该令牌:

Then I used that token in PowerShell:

PS C:\Users\shtabriz> Connect-MsolService -AccessToken eyJ0eXAiOiJKV1QiLCJ...
PS C:\Users\shtabriz> Get-MsolUser

UserPrincipalName                                     DisplayName       isLicensed
-----------------                                     -----------       ----------
test@shawntest.onmicrosoft.com                        TestMe            False
shtabriz_microsoft.com#EXT#@shawntest.onmicrosoft.com Shawn Tabrizi     False
admin@shawntest.onmicrosoft.com                       ShawnTabriziAdmin False
Alex@shawntest.onmicrosoft.com                        Alex Wu           False
language@shawntest.onmicrosoft.com                    Language          False
languageportal@shawntest.onmicrosoft.com              Language Portal   False

您将获得哪种访问令牌?您愿意分享吗?还是至少有JWT的未签名的反编译版本?

What kind of access token are you getting? Would you mind sharing it? Or at least a JWT decompiled version of it without a signature?

如果您想把它带到其他地方,请随时通过

If you want to take this elsewhere, feel free to reach out to us at

AADPowerShellPreview@microsoft.com

AADPowerShellPreview@microsoft.com

请包括在此处找到的日志文件:

Please include the log files found here:

C:\ Users [您的别名] \ AppData \ Local \ Microsoft \ Office365 \ Powershell

C:\Users[youralias]\AppData\Local\Microsoft\Office365\Powershell

这篇关于“意外错误"使用Connect-MsolService -AccessToken连接到Azure AD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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