How to use SolrNet in a Solr that require authentication

1,655 views
Skip to first unread message

Lucas

unread,
May 14, 2013, 2:42:39 PM5/14/13
to sol...@googlegroups.com
Hi, I'm trying to access the Solr via SolrNet but the WebServer where is the Solr is a Apache/TomCat and is configured to require authentication, so, when I try to access the Solr via SolrNet i receive the message:


"

HTTP Status 401 - Authentication required.

"

How can I do that on the better way?

Thanks.

Mauricio Scheffer

unread,
May 14, 2013, 2:52:06 PM5/14/13
to sol...@googlegroups.com


--
Mauricio


--
You received this message because you are subscribed to the Google Groups "SolrNet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to solrnet+u...@googlegroups.com.
To post to this group, send email to sol...@googlegroups.com.
Visit this group at http://groups.google.com/group/solrnet?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Lucas

unread,
May 14, 2013, 3:10:51 PM5/14/13
to sol...@googlegroups.com
Hi Mauricio,

I read it all but o could not implement the authentication... i see something like:

SolrNet.ISolrConnection connection = new SolrConnection("http://127.0.0.1:8080/solr"
            {
                HttpWebRequestFactory = new DemoHttpWebRequestFactory()
            };

But i could not implement it, i didn't understand, can you give me a example?

Thanks

Mauricio Scheffer

unread,
May 14, 2013, 3:12:02 PM5/14/13
to sol...@googlegroups.com
There's no such thing as DemoHttpWebRequestFactory. Please read the page to the end.


--
Mauricio

Lucas

unread,
May 14, 2013, 3:23:35 PM5/14/13
to sol...@googlegroups.com
Sorry, i read it all but i couldn't understand when can i do that...

I have in Global.asax:

         SolrNet.Startup.Init<Class>("address");

And when i do this in any page:

        var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Class>>();
        SolrNet.SolrQueryResults<Class> result;
        result = solr.Query(new SolrQuery(query));

On the last line i receive the error... but i already read the page you send me but i couldn't understand how to do this work...

Can you help me?

Daniel Pincas

unread,
May 14, 2013, 3:40:24 PM5/14/13
to sol...@googlegroups.com
Hi Lucas,

We recently did something like this in a little app we're building. I believe you have to implement IHttpWebRequestFactory in your application. So something simple like:

public class YourSolrWebRequestFactory : IHttpWebRequestFactory
{
    public IHttpWebRequest Create(Uri url)
    {
        var webrequest = (HttpWebRequest)WebRequest.Create(url);
        var wrCache = new CredentialCache();
        wrCache.Add(url, "Basic", new NetworkCredential("yourusername", "yourpassword"));
        webrequest.Credentials = wrCache;
        return new HttpWebRequestAdapter(webrequest);
    }
}

Note that this example is using HTTP basic authentication. If you're connecting to your SOLR server via HTTPS this should be acceptable. In our situation we're using the Jetty servlet container that comes bundled with Solr.

Then in your global.asax:

var connection = new SolrConnection("https://your/solr/core");
connection.HttpWebRequestFactory = new YourSolrWebRequestFactory();
Startup.Init<YourSolrViewModel>(connection);

And that's it!

Cheers,

Dan


Lucas

unread,
May 14, 2013, 4:21:12 PM5/14/13
to sol...@googlegroups.com
Dan Picas, thank you very much! That worked! :)

Mauricio Scheffer

unread,
May 14, 2013, 5:13:17 PM5/14/13
to sol...@googlegroups.com
Hi guys, there's no need to write your own implementation of IHttpWebRequestFactory for basic auth anymore. As explained on the issue page, a BasicAuthHttpWebRequestFactory class is now included in SolrNet.

Cheers



--
Mauricio

Landon Campbell

unread,
Jun 10, 2015, 4:25:40 PM6/10/15
to sol...@googlegroups.com
Hey Dan,

How can you get this to work if you're using HAProxy in front of a SolrCloud? I'm still getting 401 using this method, but I'm guessing HAProxy isn't passing the credentials through?

Thanks,
Landon

Pandurang Pailvan

unread,
Jun 20, 2019, 8:23:53 AM6/20/19
to SolrNet
Hi Daniel Pincas,

Thanks a lot it helped me lot.
To unsubscribe from this group and stop receiving emails from it, send an email to sol...@googlegroups.com.

Long Trịnh

unread,
Feb 9, 2021, 4:28:29 AM2/9/21
to SolrNet
Hi all,

Can Anyone help me to use basic authentication with SolrCloud? I cannot use SolrConnection for connect to zookeeper client. Thanks
var zookeeperConnectionString = "127.0.0.1:2181"; 
  var collectionName = "collection_name"; 
  Startup.Init<Product>(new SolrCloudStateProvider(zookeeperConnectionString), collectionName);

Samuel Fung

unread,
Jun 21, 2021, 4:23:38 AM6/21/21
to SolrNet
Dear Mauricio,

I use SolrNet.Microsoft.DependencyInjection. I've tried the following, but still have 401. Can you please give some hints how to inject BasicAuthHttpWebRequestFactory?

services.AddSolrNet<SolrDoc>("http://localhost:8983/solr/mycore");
services.AddSingleton<IHttpWebRequestFactory>(new BasicAuthHttpWebRequestFactory("solr", "SolrRocks"));

Regards,
Samuel

Mauricio Scheffer 在 2013年5月15日 星期三上午5:13:17 [UTC+8] 的信中寫道:

Mauricio Scheffer

unread,
Jul 5, 2021, 5:19:48 PM7/5/21
to sol...@googlegroups.com
Hi Samuel, use the setupAction parameter of the AddSolrNet method.

--
Mauricio


Samuel Fung

unread,
Jul 10, 2021, 1:38:53 AM7/10/21
to SolrNet
Thanks Mauricio. I've tried the following, still 401.

services.AddSolrNet<SolrDoc>("http://localhost:8983/solr/mycore", options =>
{
    var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("solr:SolrRocks"));
    options.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
});

Mauricio Scheffer 在 2021年7月6日 星期二上午5:19:48 [UTC+8] 的信中寫道:

Mauricio Scheffer

unread,
Jul 15, 2021, 7:04:26 PM7/15/21
to sol...@googlegroups.com
Hi, this works for me, try using it as reference for your own code:


--
Mauricio


Samuel Fung

unread,
Sep 16, 2021, 6:19:49 AM9/16/21
to SolrNet
Hi,

Reading the src, I think basic auth with SolrCloud is not supported

We can workaround by replacing ISolrOperationsProvider,

public class BasicAuthOperationsProvider : ISolrOperationsProvider
{
private string _user;
private string _password;

public BasicAuthOperationsProvider(string user, string password)
{
_user = user;
_password = password;
}

public ISolrBasicOperations<T> GetBasicOperations<T>(string url, bool isPostConnection = false)
{
var conn = new SolrConnection(url)
{
HttpWebRequestFactory = new BasicAuthHttpWebRequestFactory(_user, _password)
};
return SolrNet.SolrNet.GetBasicServer<T>(conn);
}

public ISolrOperations<T> GetOperations<T>(string url, bool isPostConnection = false)
{
var conn = new SolrConnection(url)
{
HttpWebRequestFactory = new BasicAuthHttpWebRequestFactory(_user, _password)
};
return SolrNet.SolrNet.GetServer<T>(conn);
}
}

SolrNet.Cloud.Startup.InitAsync<SolrDoc>(new SolrCloudStateProvider("localhost:9983"), "nextore").Wait();
SolrNet.Startup.Container.Remove<ISolrOperationsProvider>();
SolrNet.Startup.Container.Register<ISolrOperationsProvider>(c => new BasicAuthOperationsProvider("solr", "SolrRocks"));
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrDoc>>();

trinh...@gmail.com 在 2021年2月9日 星期二下午5:28:29 [UTC+8] 的信中寫道:

Samuel Fung

unread,
Sep 16, 2021, 6:26:25 AM9/16/21
to SolrNet
Thanks Mauricio, finally I figure out only async method can use basic auth.
Mauricio Scheffer 在 2021年7月16日 星期五上午7:04:26 [UTC+8] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages