Differences in length of access token generated from PowerShell and C# .Net library of PowerShell respectively

左心房为你撑大大i 提交于 2021-02-11 14:10:25

问题


I am new in Azure DevOps, I am acquiring access token of Azure DevOps by two ways

  1. Windows PowerShell
  2. PowerShell within WPF application

In above two ways, I am able to get access token but there is a big difference in size of access token. If I follow the 1st method I am getting full access token and it works fine. But if I use the 2nd method, it returns access token with small size, resulting I get a 401 error when I use this access token.

I am sharing outputs from both ways

1. Using PowerShell

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlNzWnNCTmhaY0YzUTlTNHRycFFCVEJ5TlJSSSIsImtpZCI6IlNzWnNCTmhaY0YzUTlTNHRycFFCVEJLKSADJFL893W9FU87FG8SF79GSG89D7S89A8F7DA98DSF7FD87ASDFFASASDMFa8dfafa8d98asfJKHKJhkHKJytrULlkjkytu78&*6i0uiuiujkUI77o9975u57789990huhihiuheRJRRJEKTsSSsrEjrzrUizrrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&rrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&rrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&rrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&aoi988988u9mZhbWlseV9uYW1lIjoiSmFpbiIsImdpdmVuX25hbWUiOiJBc2hpc2giLCJncm91cHMiOlsiNGMyOGQ1OTktZWE0Yy00NzMwLWJjOTQtNzk2ZjA1YzU4OGUzIiwiMmE0YWU5NDAtYWVmOS00OWViLTkyMWQtYTA1OTNmNzE1MzdjIiwiMWY3YjZiYzctNGE2NC00NDU3LThjNmYtZjdmZjU5ZDFkZmQ0IiwiNzA1M2VhYjgtMjgzNS00YmFmLWE1MWEtMjlkM2Q0NzY1NzhjIiwiODc3ZWU1OWMtYzhlMC00YmM4LTgxY2QtZTIwMTdlNm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1NiIsIng1dCI6IlNzWnNCTmhaY0YzUTlTNHRycFFCVEJ5TlJSSSIsImtpZCI6IlNzWnNCTmhaY0YzUTlTNHRycFFCVEJ5TlJSSSJ9.eyJhdWQiOiJodHRwczovL21hbmFnZW1lbnQuY29yZS53aW5kb3dzLm5ldC8iLCJpc3MiOiJodHRwcLKSADJFL893W9FU87FG8SF79GSG89D7S89A8F7DA98DSF7FD87ASDFFASASDMFa8dfafa8d98asfJKHKJhkHKJytrULlkjkytu78&J6i0uiuiujkUI77o9975u57789990huhihiuheRJRRJEKTsSSsrEjrzrUizrrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&rrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&rrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&rrTRjkz89394348Q589990huhihiuheRJRRJEKTsSSsrEjrzrUizrrTRjkz89394348Q5NTk0NWZjMiIsImFwcGSG89D7S89A8F7DA98DSF7FD8kytu78&rrTRjkz89394348Q5NTk0NWZNTk0NWZjMiIsImFwcGSG89D7SQ2MTMtOGRmOC00MjViY2Q5Y2U5ODQiLCJ1dGkiOiJpa2VueHFhSHEwLVlOczFNSHE4TkFRIiwidmVyIjoiMS4wIn0.Cl1eMNG_M1iTQzMiM6YJmaBx664_0-Bt_BnCOViKtq-7tRWq5n1PVDjaF-LhkpbzeU5YlQfyK8Y7tkTIg8gwuZJwd2jeiBKVeyriOFOv562OSDCRdguGiqnhZnIpjXIsqXZIqBgb8dDZJCeqrAZ0tdThSe3yiEZ0Ni98JhrltyiZ3SegocrtgHm8PTmYgk05v3d5mrfS1H9v-szuFn8-AWRmtG_3ipiUVpRE2v5MyVzY8D_1_vE_xKknXG2i52c_ltJTpE4uLGjQ3Uw4T6zhP4xbIBzrLynJjxHfzC6DMsPkNk-3SNdtCo9lsR2fj6m1_fOhfAbWknI4oCN2VLB69w

You can see the differences in the length of the access token.

My C# code is

public string GetAccessToken()
{
    string accesstoken = "";
    try
    {
        using (PowerShell PowerShellInstance = PowerShell.Create())
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(@"Clear-AzureRmContext -Force;");
            stringBuilder.Append(@"$password = ConvertTo-SecureString -String """ + thisApp.xmlSettings.ClientKey +"\""+" -AsPlainText -Force;");
            stringBuilder.Append(@"$cred = New-Object System.Management.Automation.PSCredential("""+thisApp.xmlSettings.ClientId+"\""+",$password);");
            stringBuilder.Append(@"Connect-AzureRmAccount -ServicePrincipal -Credential $cred -Tenant """+thisApp.xmlSettings.TenantId+"\""+";");
            stringBuilder.Append(@"$currentAzureContext = Get-AzureRmContext;$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile;$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile);$pat = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId).AccessToken; Write-Output $pat");

            PowerShellInstance.AddScript(stringBuilder.ToString());

            // prepare a new collection to store output stream objects
            PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
            // use this overload to specify an output stream buffer
            IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);

            // do something else until execution has completed.
            // this could be sleep/wait, or perhaps some other work
            while (result.IsCompleted == false)
            {
                Thread.Sleep(1000);
            }
            accesstoken = outputCollection[1].ToString();
        }
    }
    catch (Exception p)
    {
        MessageBox.Show(p.Message + "\n" + p.InnerException);
    }
    return accesstoken;
}

Can anyone let me know why it is that? why this much difference is there in access token?

来源:https://stackoverflow.com/questions/62452225/differences-in-length-of-access-token-generated-from-powershell-and-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!