Ejemplo n.º 1
0
        static HttpContent ParseHttpContent(string verb, CommandLineParameters parameters)
        {
            bool requiresData = String.Equals(verb, "put", StringComparison.OrdinalIgnoreCase) ||
                                String.Equals(verb, "patch", StringComparison.OrdinalIgnoreCase);
            bool inputRedirected = Console.IsInputRedirected;

            if (requiresData || String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase))
            {
                string data = parameters.Get("2", "content", requires: requiresData && !inputRedirected);
                if (data == null)
                {
                    if (inputRedirected)
                    {
                        return(new StringContent(Console.In.ReadToEnd(), Encoding.UTF8, Constants.JsonContentType));
                    }

                    return(new StringContent(String.Empty, Encoding.UTF8, Constants.JsonContentType));
                }

                if (data.StartsWith("@"))
                {
                    data = File.ReadAllText(data.Substring(1));
                }

                return(new StringContent(data, Encoding.UTF8, Constants.JsonContentType));
            }
            return(null);
        }
Ejemplo n.º 2
0
        static int Main(string[] args)
        {
            // ensure Tls12
            if ((ServicePointManager.SecurityProtocol & SecurityProtocolType.Tls12) != SecurityProtocolType.Tls12)
            {
                ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
            }

            if (Utils.GetSkipSslVerify())
            {
                ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };
            }

            Utils.SetTraceListener(new ConsoleTraceListener());
            try
            {
                var persistentAuthHelper = new PersistentAuthHelper();
                if (args.Length > 0)
                {
                    var _parameters = new CommandLineParameters(args);
                    var verb        = _parameters.Get(0, "verb");
                    if (String.Equals(verb, "login", StringComparison.OrdinalIgnoreCase))
                    {
                        var env = _parameters.Get(1, requires: false);
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.SetAzureEnvironment(!string.IsNullOrEmpty(env) ? env : Utils.GetDefaultEnv());
                        persistentAuthHelper.AcquireTokens().Wait();
                        return(0);
                    }
                    else if (String.Equals(verb, "azlogin", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.SetAzureEnvironment(Constants.ARMProdEnv);
                        persistentAuthHelper.AzLogin().Wait();
                        return(0);
                    }
                    else if (String.Equals(verb, "listcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        EnsureTokenCache(persistentAuthHelper);

                        foreach (var line in persistentAuthHelper.DumpTokenCache())
                        {
                            Console.WriteLine(line);
                        }
                        return(0);
                    }
                    else if (String.Equals(verb, "clearcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        persistentAuthHelper.ClearTokenCache();
                        return(0);
                    }
                    else if (String.Equals(verb, "token", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, requires: false);

                        if (tenantId == null)
                        {
                            var accessToken = Utils.GetDefaultToken();
                            if (!String.IsNullOrEmpty(accessToken))
                            {
                                _parameters.ThrowIfUnknown();
                                DumpClaims(accessToken);
                                Console.WriteLine();
                                return(0);
                            }
                        }

                        if (tenantId != null && tenantId.StartsWith("ey"))
                        {
                            _parameters.ThrowIfUnknown();
                            DumpClaims(tenantId);
                            return(0);
                        }

                        EnsureTokenCache(persistentAuthHelper);

                        TokenCacheInfo cacheInfo;
                        if (Uri.TryCreate(tenantId, UriKind.Absolute, out _))
                        {
                            // https://vault.azure.net (no trailing /)
                            // https://graph.windows.net (no trailing /)
                            // https://management.core.windows.net/
                            _parameters.ThrowIfUnknown();
                            cacheInfo = persistentAuthHelper.GetTokenByResource(tenantId).Result;
                        }
                        else
                        {
                            var resource = _parameters.Get(2, requires: false);
                            if (!string.IsNullOrEmpty(resource) && !Uri.TryCreate(resource, UriKind.Absolute, out _))
                            {
                                throw new CommandLineException($"Parameter '{resource}' must be resource uri!");
                            }

                            _parameters.ThrowIfUnknown();
                            cacheInfo = persistentAuthHelper.GetToken(tenantId, resource).Result;
                        }

                        var bearer = cacheInfo.CreateAuthorizationHeader();
                        Clipboard.SetText(cacheInfo.AccessToken);
                        DumpClaims(cacheInfo.AccessToken);
                        Console.WriteLine();
                        Console.WriteLine("Token copied to clipboard successfully.");
                        return(0);
                    }
                    else if (String.Equals(verb, "spn", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, keyName: "tenant");
                        var appId    = _parameters.Get(2, keyName: "appId");
                        EnsureGuidFormat(appId);

                        X509Certificate2 certificate = null;
                        var    appKey   = _parameters.Get(3, keyName: "appKey", requires: false);
                        string resource = null;
                        if (appKey != null && appKey.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
                        {
                            resource = appKey;
                            appKey   = _parameters.Get(4, keyName: "appKey", requires: false);
                        }

                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        else
                        {
                            if (File.Exists(appKey))
                            {
                                var password = _parameters.Get(4, keyName: "password", requires: false);
                                if (password == null)
                                {
                                    password = appKey + ".txt";
                                    if (!File.Exists(password))
                                    {
                                        password = PromptForPassword("password");
                                    }
                                }

                                if (File.Exists(password))
                                {
                                    certificate = new X509Certificate2(appKey, File.ReadAllText(password));
                                }
                                else
                                {
                                    certificate = new X509Certificate2(appKey, password);
                                }
                            }
                        }

                        if (certificate == null)
                        {
                            appKey = Utils.EnsureBase64Key(appKey);
                        }

                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.SetAzureEnvironment(Utils.GetDefaultEnv());
                        var cacheInfo = certificate != null?
                                        persistentAuthHelper.GetTokenBySpn(tenantId, appId, certificate, resource).Result:
                                        persistentAuthHelper.GetTokenBySpn(tenantId, appId, appKey, resource).Result;

                        return(0);
                    }
                    else if (String.Equals(verb, "upn", StringComparison.OrdinalIgnoreCase))
                    {
                        var username = _parameters.Get(1, keyName: "username");
                        var password = _parameters.Get(2, keyName: "password", requires: false);
                        if (password == null)
                        {
                            password = PromptForPassword("password");
                        }
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.SetAzureEnvironment(Utils.GetDefaultEnv());
                        var cacheInfo = persistentAuthHelper.GetTokenByUpn(username, password).Result;
                        return(0);
                    }
                    else if (String.Equals(verb, "get", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "delete", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "put", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "patch", StringComparison.OrdinalIgnoreCase))
                    {
                        var path    = _parameters.Get(1, keyName: "url");
                        var verbose = _parameters.Get("-verbose", requires: false) != null || Utils.GetDefaultVerbose();
                        if (!verbose)
                        {
                            Trace.Listeners.Clear();
                        }

                        var content = ParseHttpContent(verb, _parameters);
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        var http2   = _parameters.Get("-http2", requires: false) != null;
                        _parameters.ThrowIfUnknown();

                        var uri         = Utils.EnsureAbsoluteUri(path, persistentAuthHelper);
                        var accessToken = Utils.GetDefaultToken();
                        if (!String.IsNullOrEmpty(accessToken))
                        {
                            return(HttpInvoke(uri, new TokenCacheInfo {
                                AccessToken = accessToken
                            }, verb, verbose, content, headers, http2).Result);
                        }

                        var env = ARMConfiguration.GetEnvironmentByRequest(uri) ?? Utils.GetDefaultEnv();
                        if (!persistentAuthHelper.IsCacheValid() || !string.Equals(env, persistentAuthHelper.ARMConfiguration.AzureEnvironment, StringComparison.OrdinalIgnoreCase))
                        {
                            persistentAuthHelper.SetAzureEnvironment(env);
                            persistentAuthHelper.AcquireTokens().Wait();
                        }

                        var resource       = GetResource(uri, persistentAuthHelper.ARMConfiguration);
                        var subscriptionId = GetTenantOrSubscription(uri);
                        var cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result ?? persistentAuthHelper.GetTokenByResource(resource).Result;
                        return(HttpInvoke(uri, cacheInfo, verb, verbose, content, headers, http2).Result);
                    }
                    else
                    {
                        throw new CommandLineException(String.Format("Parameter '{0}' is invalid!", verb));
                    }
                }

                PrintUsage();
                return(1);
            }
            catch (Exception ex)
            {
                DumpException(ex);
                return(-1);
            }
        }
Ejemplo n.º 3
0
        static int Main(string[] args)
        {
            //Utils.SetTraceListener(new ConsoleTraceListener());
            try
            {
                var persistentAuthHelper = new PersistentAuthHelper();
                persistentAuthHelper.AzureEnvironments = AzureEnvironments.Prod;
                if (args.Length > 0)
                {
                    var _parameters = new CommandLineParameters(args);
                    var verb        = _parameters.Get(0, "verb");
                    if (String.Equals(verb, "login", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        persistentAuthHelper.AcquireTokens().Wait();
                        return(0);
                    }
                    else if (String.Equals(verb, "spn", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, keyName: "tenant");
                        var appId    = _parameters.Get(2, keyName: "appId");
                        EnsureGuidFormat(appId);

                        X509Certificate2 certificate = null;
                        var appKey = _parameters.Get(3, keyName: "appKey", requires: false);
                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        else
                        {
                            if (File.Exists(appKey))
                            {
                                var password = _parameters.Get(4, keyName: "password", requires: false);
                                if (password == null)
                                {
                                    password = PromptForPassword("password");
                                }

                                certificate = new X509Certificate2(appKey, password);
                            }
                        }

                        if (certificate == null)
                        {
                            appKey = Utils.EnsureBase64Key(appKey);
                        }

                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();
                        var info = certificate != null?
                                   AADHelper.AcquireTokenByX509(tenantId, appId, certificate).Result:
                                   AADHelper.AcquireTokenBySPN(tenantId, appId, appKey).Result;

                        //Clipboard.SetText(info.access_token);
                        DumpClaims(info.access_token);
                        Console.WriteLine();
                        Console.WriteLine("Token copied to clipboard successfully.");
                        return(0);
                    }
                    else if (String.Equals(verb, "get-tenant", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/tenantDetails?api-version=1.6", tenant);
                        var uri  = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    else if (String.Equals(verb, "get-tenant", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/tenantDetails/{0}?api-version=1.6", tenant);
                        var uri  = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    else if (String.Equals(verb, "get-apps", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/applications?api-version=1.6", tenant);
                        var uri  = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    // https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/
                    // https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console/blob/master/GraphConsoleAppV3/Program.cs
                    else if (String.Equals(verb, "add-app", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var app     = _parameters.Get(2, keyName: "app");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        Guid unused;
                        var  isGuid = Guid.TryParse(app, out unused);

                        var path = isGuid ? String.Format("/{0}/applications?$filter=appId eq '{1}'&api-version=1.6", tenant, app)
                            : String.Format("/{0}/applications?$filter=displayName eq '{1}'&api-version=1.6", tenant, app);

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    else if (String.Equals(verb, "get-app", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var app     = _parameters.Get(2, keyName: "app");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        Guid unused;
                        var  isGuid = Guid.TryParse(app, out unused);

                        var path = isGuid ? String.Format("/{0}/applications?$filter=appId eq '{1}'&api-version=1.6", tenant, app)
                            : String.Format("/{0}/applications?$filter=displayName eq '{1}'&api-version=1.6", tenant, app);

                        var            uri            = EnsureAbsoluteUri(path, persistentAuthHelper);
                        var            resource       = GetResource(uri);
                        var            subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    // https://msdn.microsoft.com/library/azure/ad/graph/api/entity-and-complex-type-reference#serviceprincipalentity
                    else if (String.Equals(verb, "get-spns", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var app     = _parameters.Get(2, keyName: "app");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        Guid appGuid = new Guid(app);
                        var  path    = String.Format("/{0}/applications/{1}/serviceprincipal?api-version=1.6", tenant, appGuid);

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    else if (String.Equals(verb, "add-cred", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        var app    = _parameters.Get(2, keyName: "app");
                        X509Certificate2 certificate = null;
                        var appKey = _parameters.Get(3, keyName: "appKey", requires: false);
                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        else
                        {
                            if (File.Exists(appKey))
                            {
                                certificate = new X509Certificate2(appKey);
                                if (certificate.HasPrivateKey)
                                {
                                    throw new Exception("Certificate must not contain private key!");
                                }
                            }
                        }

                        if (certificate == null)
                        {
                            appKey = Utils.EnsureBase64Key(appKey);
                        }

                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var         appObject   = GetAppObject(persistentAuthHelper, tenant, app).Result;
                        var         appObjectId = GetAppObjectId(appObject);
                        HttpContent content;
                        if (certificate != null)
                        {
                            content = GetPatchContent(appObject, certificate);
                        }
                        else
                        {
                            content = GetPatchContent(appObject, appKey);
                        }

                        var path = String.Format("/{0}/directoryObjects/{1}/Microsoft.DirectoryServices.Application?api-version=1.6", tenant, appObjectId);

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;

                        return(HttpInvoke(uri, cacheInfo, "patch", Utils.GetDefaultVerbose(), content, headers).Result);
                    }
                    else if (String.Equals(verb, "del-cred", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        var app    = _parameters.Get(2, keyName: "app");
                        var keyId  = _parameters.Get(3, keyName: "keyId");
                        EnsureGuidFormat(keyId);

                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var appObject   = GetAppObject(persistentAuthHelper, tenant, app).Result;
                        var appObjectId = GetAppObjectId(appObject);
                        var content     = GetRemoveContent(appObject, keyId);
                        var path        = String.Format("/{0}/directoryObjects/{1}/Microsoft.DirectoryServices.Application?api-version=1.6", tenant, appObjectId);

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;

                        return(HttpInvoke(uri, cacheInfo, "patch", Utils.GetDefaultVerbose(), content, headers).Result);
                    }
                    else if (String.Equals(verb, "get-users", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/users?api-version=1.6", tenant);
                        var uri  = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    else if (String.Equals(verb, "get-user", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var user    = _parameters.Get(2, keyName: "user");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/users/{1}?api-version=1.6", tenant, user);
                        if ((user.StartsWith("1") || user.StartsWith("0")) && user.Length == 16)
                        {
                            path = String.Format("/{0}/users?api-version=1.2-internal&$filter=netId eq '{1}' or alternativeSecurityIds/any(x:x/type eq 1 and x/identityProvider eq null and x/key eq X'{1}')", tenant, user);
                        }
                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null, headers).Result);
                    }
                    else if (String.Equals(verb, "get-groups", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant  = _parameters.Get(1, keyName: "tenant");
                        var user    = _parameters.Get(2, keyName: "user");
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();


                        var path = String.Format("/{0}/users/{1}/getMemberGroups?api-version=1.6", tenant, user);
                        var uri  = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        var            resource       = GetResource(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        var            content        = new StringContent("{\"securityEnabledOnly\": false}", Encoding.UTF8, "application/json");
                        return(HttpInvoke(uri, cacheInfo, "post", Utils.GetDefaultVerbose(), content, headers).Result);
                    }
                    else
                    {
                        throw new CommandLineException(String.Format("Parameter '{0}' is invalid!", verb));
                    }
                }

                PrintUsage();
                return(1);
            }
            catch (Exception ex)
            {
                DumpException(ex);
                return(-1);
            }
        }
Ejemplo n.º 4
0
        static HttpContent ParseHttpContent(string verb, CommandLineParameters parameters)
        {
            bool requiresData = String.Equals(verb, "put", StringComparison.OrdinalIgnoreCase)
                        || String.Equals(verb, "patch", StringComparison.OrdinalIgnoreCase);
            bool inputRedirected = Console.IsInputRedirected;

            if (requiresData || String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase))
            {
                string data = parameters.Get("2", "content", requires: requiresData && !inputRedirected);
                if (data == null)
                {
                    if (inputRedirected)
                    {
                        return new StringContent(Console.In.ReadToEnd(), Encoding.UTF8, Constants.JsonContentType);
                    }

                    return new StringContent(String.Empty, Encoding.UTF8, Constants.JsonContentType);
                }

                if (data.StartsWith("@"))
                {
                    data = File.ReadAllText(data.Substring(1));
                }

                return new StringContent(data, Encoding.UTF8, Constants.JsonContentType);
            }
            return null;
        }
Ejemplo n.º 5
0
        static int Main(string[] args)
        {
            Utils.SetTraceListener(new ConsoleTraceListener());
            try
            {
                var persistentAuthHelper = new PersistentAuthHelper();
                persistentAuthHelper.AzureEnvironments = AzureEnvironments.Prod;
                if (args.Length > 0)
                {
                    var _parameters = new CommandLineParameters(args);
                    var verb = _parameters.Get(0, "verb");
                    if (String.Equals(verb, "login", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        persistentAuthHelper.AcquireTokens().Wait();
                        return 0;
                    }
                    else if (String.Equals(verb, "spn", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, keyName: "tenant");
                        var appId = _parameters.Get(2, keyName: "appId");
                        EnsureGuidFormat(appId);

                        X509Certificate2 certificate = null;
                        var appKey = _parameters.Get(3, keyName: "appKey", requires: false);
                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        else
                        {
                            if (File.Exists(appKey))
                            {
                                var password = _parameters.Get(4, keyName: "password", requires: false);
                                if (password == null)
                                {
                                    password = PromptForPassword("password");
                                }

                                certificate = new X509Certificate2(appKey, password);
                            }
                        }

                        if (certificate == null)
                        {
                            appKey = Utils.EnsureBase64Key(appKey);
                        }

                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();
                        var info = certificate != null ?
                            AADHelper.AcquireTokenByX509(tenantId, appId, certificate).Result :
                            AADHelper.AcquireTokenBySPN(tenantId, appId, appKey).Result;
                        Clipboard.SetText(info.access_token);
                        DumpClaims(info.access_token);
                        Console.WriteLine();
                        Console.WriteLine("Token copied to clipboard successfully.");
                        return 0;
                    }
                    else if (String.Equals(verb, "get-tenant", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/tenantDetails?api-version=1.6", tenant);
                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(subscriptionId).Result;
                        return HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null).Result;
                    }
                    else if (String.Equals(verb, "get-tenant", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/tenantDetails/{0}?api-version=1.6", tenant);
                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(subscriptionId).Result;
                        return HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null).Result;
                    }
                    else if (String.Equals(verb, "get-apps", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        _parameters.ThrowIfUnknown();

                        var path = String.Format("/{0}/applications?api-version=1.6", tenant);
                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(subscriptionId).Result;
                        return HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null).Result;
                    }
                    else if (String.Equals(verb, "get-app", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        var app = _parameters.Get(2, keyName: "app");
                        _parameters.ThrowIfUnknown();

                        Guid unused;
                        var isGuid = Guid.TryParse(app, out unused);

                        var path = isGuid ? String.Format("/{0}/applications?$filter=appId eq '{1}'&api-version=1.6", tenant, app)
                            : String.Format("/{0}/applications?$filter=displayName eq '{1}'&api-version=1.6", tenant, app);

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(subscriptionId).Result;
                        return HttpInvoke(uri, cacheInfo, "get", Utils.GetDefaultVerbose(), null).Result;
                    }
                    else if (String.Equals(verb, "add-cred", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        var app = _parameters.Get(2, keyName: "app");
                        X509Certificate2 certificate = null;
                        var appKey = _parameters.Get(3, keyName: "appKey", requires: false);
                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        else
                        {
                            if (File.Exists(appKey))
                            {
                                certificate = new X509Certificate2(appKey);
                                if (certificate.HasPrivateKey)
                                {
                                    throw new Exception("Certificate must not contain private key!");
                                }
                            }
                        }

                        if (certificate == null)
                        {
                            appKey = Utils.EnsureBase64Key(appKey);
                        }

                        _parameters.ThrowIfUnknown();

                        var appObject = GetAppObject(persistentAuthHelper, tenant, app).Result;
                        var appObjectId = GetAppObjectId(appObject);
                        HttpContent content;
                        if (certificate != null)
                        {
                            content = GetPatchContent(appObject, certificate);
                        }
                        else
                        {
                            content = GetPatchContent(appObject, appKey);
                        }

                        var path = String.Format("/{0}/directoryObjects/{1}/Microsoft.DirectoryServices.Application?api-version=1.6", tenant, appObjectId);

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(subscriptionId).Result;

                        return HttpInvoke(uri, cacheInfo, "patch", Utils.GetDefaultVerbose(), content).Result;
                    }
                    else if (String.Equals(verb, "del-cred", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenant = _parameters.Get(1, keyName: "tenant");
                        var app = _parameters.Get(2, keyName: "app");
                        var keyId = _parameters.Get(3, keyName: "keyId");
                        EnsureGuidFormat(keyId);
                        _parameters.ThrowIfUnknown();

                        var appObject = GetAppObject(persistentAuthHelper, tenant, app).Result;
                        var appObjectId = GetAppObjectId(appObject);
                        var content = GetRemoveContent(appObject, keyId);
                        var path = String.Format("/{0}/directoryObjects/{1}/Microsoft.DirectoryServices.Application?api-version=1.6", tenant, appObjectId);

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);

                        var subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(subscriptionId).Result;

                        return HttpInvoke(uri, cacheInfo, "patch", Utils.GetDefaultVerbose(), content).Result;
                    }
                    else
                    {
                        throw new CommandLineException(String.Format("Parameter '{0}' is invalid!", verb));
                    }
                }

                PrintUsage();
                return 1;
            }
            catch (Exception ex)
            {
                DumpException(ex);
                return -1;
            }
        }
Ejemplo n.º 6
0
        static int Main(string[] args)
        {
            Utils.SetTraceListener(new ConsoleTraceListener());
            try
            {
                var persistentAuthHelper = new PersistentAuthHelper();
                if (args.Length > 0)
                {
                    var _parameters = new CommandLineParameters(args);
                    var verb = _parameters.Get(0, "verb");
                    if (String.Equals(verb, "login", StringComparison.OrdinalIgnoreCase))
                    {
                        var env = _parameters.Get(1, requires: false);
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = env == null ? Utils.GetDefaultEnv() :
                            (AzureEnvironments)Enum.Parse(typeof(AzureEnvironments), args[1], ignoreCase: true);
                        persistentAuthHelper.AcquireTokens().Wait();
                        return 0;
                    }
                    else if (String.Equals(verb, "listcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        EnsureTokenCache(persistentAuthHelper);

                        foreach (var line in persistentAuthHelper.DumpTokenCache())
                        {
                            Console.WriteLine(line);
                        }
                        return 0;
                    }
                    else if (String.Equals(verb, "clearcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        persistentAuthHelper.ClearTokenCache();
                        return 0;
                    }
                    else if (String.Equals(verb, "token", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, requires: false);
                        _parameters.ThrowIfUnknown();

                        if (tenantId != null && tenantId.StartsWith("ey"))
                        {
                            DumpClaims(tenantId);
                            return 0;
                        }

                        EnsureTokenCache(persistentAuthHelper);

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();

                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(tenantId).Result;
                        var bearer = cacheInfo.CreateAuthorizationHeader();
                        Clipboard.SetText(bearer);
                        DumpClaims(cacheInfo.AccessToken);
                        Console.WriteLine();
                        Console.WriteLine("Token copied to clipboard successfully.");
                        return 0;
                    }
                    else if (String.Equals(verb, "spn", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, keyName: "tenant");
                        var appId = _parameters.Get(2, keyName: "appId");
                        EnsureGuidFormat(appId);

                        X509Certificate2 certificate = null;
                        var appKey = _parameters.Get(3, keyName: "appKey", requires: false);
                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        else
                        {
                            if (File.Exists(appKey))
                            {
                                var password = _parameters.Get(4, keyName: "password", requires: false);
                                if (password == null)
                                {
                                    password = PromptForPassword("password");
                                }

                                certificate = new X509Certificate2(appKey, password);
                            }
                        }

                        if (certificate == null)
                        {
                            appKey = Utils.EnsureBase64Key(appKey);
                        }

                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();
                        var cacheInfo = certificate != null ?
                            persistentAuthHelper.GetTokenBySpn(tenantId, appId, certificate).Result :
                            persistentAuthHelper.GetTokenBySpn(tenantId, appId, appKey).Result;
                        return 0;
                    }
                    else if (String.Equals(verb, "upn", StringComparison.OrdinalIgnoreCase))
                    {
                        var username = _parameters.Get(1, keyName: "username");
                        var password = _parameters.Get(2, keyName: "password", requires: false);
                        if (password == null)
                        {
                            password = PromptForPassword("password");
                        }
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();
                        var cacheInfo = persistentAuthHelper.GetTokenByUpn(username, password).Result;
                        return 0;
                    }
                    else if (String.Equals(verb, "get", StringComparison.OrdinalIgnoreCase)
                        || String.Equals(verb, "delete", StringComparison.OrdinalIgnoreCase)
                        || String.Equals(verb, "put", StringComparison.OrdinalIgnoreCase)
                        || String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase)
                        || String.Equals(verb, "patch", StringComparison.OrdinalIgnoreCase))
                    {
                        var path = _parameters.Get(1, keyName: "url");
                        var verbose = _parameters.Get("-verbose", requires: false) != null || Utils.GetDefaultVerbose();
                        if (!verbose)
                        {
                            Trace.Listeners.Clear();
                        }

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);
                        var env = GetAzureEnvironments(uri, persistentAuthHelper);
                        if (!persistentAuthHelper.IsCacheValid() || persistentAuthHelper.AzureEnvironments != env)
                        {
                            persistentAuthHelper.AzureEnvironments = env;
                            persistentAuthHelper.AcquireTokens().Wait();
                        }

                        var content = ParseHttpContent(verb, _parameters);
                        _parameters.ThrowIfUnknown();

                        var subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(subscriptionId).Result;
                        return HttpInvoke(uri, cacheInfo, verb, verbose, content).Result;
                    }
                    else
                    {
                        throw new CommandLineException(String.Format("Parameter '{0}' is invalid!", verb));
                    }
                }

                PrintUsage();
                return 1;
            }
            catch (Exception ex)
            {
                DumpException(ex);
                return -1;
            }
        }
Ejemplo n.º 7
0
        static int Main(string[] args)
        {
            Utils.SetTraceListener(new ConsoleTraceListener());
            try
            {
                var persistentAuthHelper = new PersistentAuthHelper();
                if (args.Length > 0)
                {
                    var _parameters = new CommandLineParameters(args);
                    var verb        = _parameters.Get(0, "verb");
                    if (String.Equals(verb, "login", StringComparison.OrdinalIgnoreCase))
                    {
                        var env = _parameters.Get(1, requires: false);
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = env == null ? AzureEnvironments.Prod :
                                                                 (AzureEnvironments)Enum.Parse(typeof(AzureEnvironments), args[1], ignoreCase: true);
                        persistentAuthHelper.AcquireTokens().Wait();
                        return(0);
                    }
                    else if (String.Equals(verb, "listcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();

                        EnsureTokenCache(persistentAuthHelper);

                        foreach (var line in persistentAuthHelper.DumpTokenCache())
                        {
                            Console.WriteLine(line);
                        }
                        return(0);
                    }
                    else if (String.Equals(verb, "clearcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.ClearTokenCache();
                        return(0);
                    }
                    else if (String.Equals(verb, "token", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, requires: false);
                        _parameters.ThrowIfUnknown();

                        EnsureTokenCache(persistentAuthHelper);

                        if (tenantId != null)
                        {
                            if (tenantId.StartsWith("ey"))
                            {
                                DumpClaims(tenantId);
                                return(0);
                            }

                            EnsureGuidFormat(tenantId);
                        }

                        TokenCacheInfo cacheInfo = persistentAuthHelper.GetToken(tenantId, Constants.CSMResource).Result;
                        var            bearer    = cacheInfo.CreateAuthorizationHeader();
                        Clipboard.SetText(bearer);
                        DumpClaims(cacheInfo.AccessToken);
                        Console.WriteLine();
                        Console.WriteLine("Token copied to clipboard successfully.");
                        return(0);
                    }
                    else if (String.Equals(verb, "spn", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, keyName: "tenant");
                        EnsureGuidFormat(tenantId);

                        var appId = _parameters.Get(2, keyName: "appId");
                        EnsureGuidFormat(appId);

                        var appKey = _parameters.Get(3, keyName: "appKey", requires: false);
                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = AzureEnvironments.Prod;
                        var cacheInfo = persistentAuthHelper.GetTokenBySpn(tenantId, appId, appKey).Result;
                        return(0);
                    }
                    else if (String.Equals(verb, "upn", StringComparison.OrdinalIgnoreCase))
                    {
                        var username = _parameters.Get(1, keyName: "username");
                        var password = _parameters.Get(2, keyName: "password", requires: false);
                        if (password == null)
                        {
                            password = PromptForPassword("password");
                        }
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = AzureEnvironments.Prod;
                        var cacheInfo = persistentAuthHelper.GetTokenByUpn(username, password).Result;
                        return(0);
                    }
                    else if (String.Equals(verb, "get", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "delete", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "put", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase))
                    {
                        var path    = _parameters.Get(1, keyName: "url");
                        var verbose = _parameters.Get("-verbose", requires: false) != null;
                        if (!verbose)
                        {
                            Trace.Listeners.Clear();
                        }

                        var uri = EnsureAbsoluteUri(path, persistentAuthHelper);
                        if (!persistentAuthHelper.IsCacheValid())
                        {
                            persistentAuthHelper.AzureEnvironments = GetAzureEnvironments(uri);
                            persistentAuthHelper.AcquireTokens().Wait();
                        }

                        var content = ParseHttpContent(verb, _parameters);
                        _parameters.ThrowIfUnknown();

                        var            subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, null).Result;
                        return(HttpInvoke(uri, cacheInfo, verb, verbose, content).Result);
                    }
                    else
                    {
                        throw new CommandLineException(String.Format("Parameter '{0}' is invalid!", verb));
                    }
                }

                PrintUsage();
                return(1);
            }
            catch (Exception ex)
            {
                DumpException(ex);
                return(-1);
            }
        }
Ejemplo n.º 8
0
        static int Main(string[] args)
        {
            Utils.SetTraceListener(new ConsoleTraceListener());
            try
            {
                var persistentAuthHelper = new PersistentAuthHelper();
                if (args.Length > 0)
                {
                    var _parameters = new CommandLineParameters(args);
                    var verb        = _parameters.Get(0, "verb");
                    if (String.Equals(verb, "login", StringComparison.OrdinalIgnoreCase))
                    {
                        var env = _parameters.Get(1, requires: false);
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = env == null?Utils.GetDefaultEnv() :
                                                                     (AzureEnvironments)Enum.Parse(typeof(AzureEnvironments), args[1], ignoreCase: true);

                        persistentAuthHelper.AcquireTokens().Wait();
                        return(0);
                    }
                    else if (String.Equals(verb, "azlogin", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = AzureEnvironments.Prod;
                        persistentAuthHelper.AzLogin().Wait();
                        return(0);
                    }
                    else if (String.Equals(verb, "listcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        EnsureTokenCache(persistentAuthHelper);

                        foreach (var line in persistentAuthHelper.DumpTokenCache())
                        {
                            Console.WriteLine(line);
                        }
                        return(0);
                    }
                    else if (String.Equals(verb, "clearcache", StringComparison.OrdinalIgnoreCase))
                    {
                        _parameters.ThrowIfUnknown();
                        persistentAuthHelper.ClearTokenCache();
                        return(0);
                    }
                    else if (String.Equals(verb, "token", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, requires: false);
                        _parameters.ThrowIfUnknown();

                        if (tenantId == null)
                        {
                            var accessToken = Utils.GetDefaultToken();
                            if (!String.IsNullOrEmpty(accessToken))
                            {
                                DumpClaims(accessToken);
                                Console.WriteLine();
                                return(0);
                            }
                        }

                        if (tenantId != null && tenantId.StartsWith("ey"))
                        {
                            DumpClaims(tenantId);
                            return(0);
                        }

                        EnsureTokenCache(persistentAuthHelper);

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();

                        TokenCacheInfo cacheInfo;
                        Uri            resourceUri = null;
                        if (Uri.TryCreate(tenantId, UriKind.Absolute, out resourceUri))
                        {
                            // https://vault.azure.net (no trailing /)
                            // https://graph.windows.net (no trailing /)
                            // https://management.core.windows.net/
                            cacheInfo = persistentAuthHelper.GetTokenByResource(tenantId).Result;
                        }
                        else
                        {
                            cacheInfo = persistentAuthHelper.GetToken(tenantId, null).Result;
                        }

                        var bearer = cacheInfo.CreateAuthorizationHeader();
                        Clipboard.SetText(cacheInfo.AccessToken);
                        DumpClaims(cacheInfo.AccessToken);
                        Console.WriteLine();
                        Console.WriteLine("Token copied to clipboard successfully.");
                        return(0);
                    }
                    else if (String.Equals(verb, "spn", StringComparison.OrdinalIgnoreCase))
                    {
                        var tenantId = _parameters.Get(1, keyName: "tenant");
                        var appId    = _parameters.Get(2, keyName: "appId");
                        EnsureGuidFormat(appId);

                        X509Certificate2 certificate = null;
                        var appKey = _parameters.Get(3, keyName: "appKey", requires: false);
                        if (appKey == null)
                        {
                            appKey = PromptForPassword("appKey");
                        }
                        else
                        {
                            if (File.Exists(appKey))
                            {
                                var password = _parameters.Get(4, keyName: "password", requires: false);
                                if (password == null)
                                {
                                    password = PromptForPassword("password");
                                }

                                certificate = new X509Certificate2(appKey, password);
                            }
                        }

                        if (certificate == null)
                        {
                            appKey = Utils.EnsureBase64Key(appKey);
                        }

                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();
                        var cacheInfo = certificate != null?
                                        persistentAuthHelper.GetTokenBySpn(tenantId, appId, certificate).Result:
                                        persistentAuthHelper.GetTokenBySpn(tenantId, appId, appKey).Result;

                        return(0);
                    }
                    else if (String.Equals(verb, "upn", StringComparison.OrdinalIgnoreCase))
                    {
                        var username = _parameters.Get(1, keyName: "username");
                        var password = _parameters.Get(2, keyName: "password", requires: false);
                        if (password == null)
                        {
                            password = PromptForPassword("password");
                        }
                        _parameters.ThrowIfUnknown();

                        persistentAuthHelper.AzureEnvironments = Utils.GetDefaultEnv();
                        var cacheInfo = persistentAuthHelper.GetTokenByUpn(username, password).Result;
                        return(0);
                    }
                    else if (String.Equals(verb, "get", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "delete", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "put", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "post", StringComparison.OrdinalIgnoreCase) ||
                             String.Equals(verb, "patch", StringComparison.OrdinalIgnoreCase))
                    {
                        var path    = _parameters.Get(1, keyName: "url");
                        var verbose = _parameters.Get("-verbose", requires: false) != null || Utils.GetDefaultVerbose();
                        if (!verbose)
                        {
                            Trace.Listeners.Clear();
                        }

                        var content = ParseHttpContent(verb, _parameters);
                        var headers = _parameters.GetValue <Dictionary <string, List <string> > >("-h", requires: false);
                        _parameters.ThrowIfUnknown();

                        var uri         = Utils.EnsureAbsoluteUri(path, persistentAuthHelper);
                        var accessToken = Utils.GetDefaultToken();
                        if (!String.IsNullOrEmpty(accessToken))
                        {
                            return(HttpInvoke(uri, new TokenCacheInfo {
                                AccessToken = accessToken
                            }, verb, verbose, content, headers).Result);
                        }

                        var env = GetAzureEnvironments(uri, persistentAuthHelper);
                        if (!persistentAuthHelper.IsCacheValid() || persistentAuthHelper.AzureEnvironments != env)
                        {
                            persistentAuthHelper.AzureEnvironments = env;
                            persistentAuthHelper.AcquireTokens().Wait();
                        }

                        var            resource       = GetResource(uri, env);
                        var            subscriptionId = GetTenantOrSubscription(uri);
                        TokenCacheInfo cacheInfo      = persistentAuthHelper.GetToken(subscriptionId, resource).Result;
                        return(HttpInvoke(uri, cacheInfo, verb, verbose, content, headers).Result);
                    }
                    else
                    {
                        throw new CommandLineException(String.Format("Parameter '{0}' is invalid!", verb));
                    }
                }

                PrintUsage();
                return(1);
            }
            catch (Exception ex)
            {
                DumpException(ex);
                return(-1);
            }
        }