6
votes

I have implemented what I believe to the proper way to delete a database (raven running as a service), based on the 1.2 Raven Studio's way of doing it and porting the code to a windows console.

static class Program
{    
    static void Main(string[] args)
    {
        try
        {
            using (var store = new DocumentStore { ConnectionStringName = "RavenDB" }.Initialize())
            {

                var metadata = new RavenJObject();

                var factory = store.JsonRequestFactory;

                const string url = "http://localhost:8080/admin/databases/raven-products?hard-delete=true";

                var credentials = CredentialCache.DefaultCredentials;

                var convention = new DocumentConvention();

                var requestParams = new CreateHttpJsonRequestParams(store.DatabaseCommands, url, "DELETE", metadata, credentials, convention);

                var request = factory.CreateHttpJsonRequest(requestParams);

                request.ExecuteRequest();

            }

        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }

        Console.WriteLine("Press any key..");
        Console.ReadKey();

    }

}

When this code is executed , the following exception is raised.

System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() at Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func`1 getResponse) in c:\Builds\RavenDB-Unstable-v1.2\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 297 at Raven.Client.Connection.HttpJsonRequest.ReadResponseJson() in c:\Builds\RavenDB-Unstable-v1.2\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 218 at Raven.Client.Connection.HttpJsonRequest.ExecuteRequest() in c:\Builds\RavenDB-Unstable-v1.2\Raven.Client.Lightweight\Connection\HttpJsonRequest.cs:line 161 at Com.BuyEfficient.Raven.Service.Program.Main(String[] args) in c:\code\buyefficient_mvc\Com.BuyEfficient\Com.BuyEfficient.Raven .Service\Program.cs:line 39

My question is this "How do I correctly set up the credentials so that the console app is authenticated"?

Thank you, Stephen

UPDATE1 Code updated to reflect accepted answer.

1

1 Answers

5
votes

Try to use CredentialCache.DefaultCredentials, or supply the credentials of an admin user on the machine.