I'm successfully logging to a SQL Server table using stored procs and my own
context information - the whole 9 yards. Works great with SQL Server
security; chokes with 'integrated security=SSPI', etc. I get the following
error msg:
log4net:ERROR [AdoNetAppender] Could not open database connection
[workstation id=BBARNUMXP;packet size=4096;integrated security=SSPI;data
source=BMT00002;persist security info=False;initial catalog=WIAN]
System.Data.SqlClient.SqlException: Login failed for user '(null)'. Reason:
Not associated with a trusted SQL Server connection.
at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean&
isInTransaction)
at
System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnec
tionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at log4net.Appender.AdoNetAppender.InitializeDatabaseConnection()
I know I'm otherwise setting up correctly, because I can successfully invoke
the same stored proc using the same connection string through standard
ADO.NET SqlDataAdapter, DataSet, and Connection objects. I connect, no prob.
So I looked in the SDK docs and found WindowsSecurityContext and tried to
set the AdoNetAppender.SecurityContext property, but no dice. My
Application_Start() code is below, and I've tried varying the order of
events as well, all to no avail; same error. Also 2 other questions: (1) How
would I pass in the equivalent of
System.Net.CredentialCache.DefaultCredentials instead of hard-coding uid/pwd
(especially password!) ? (2) Note I pass a null to Impersonate(). Couldn't
figure out what I was supposed to do there. Could this be the problem?
I'm not very good with security issues; anyone got any ideas? Thanks in
advance.
-BillyB
William Barnum
[EMAIL PROTECTED]
P.S. I'm surprised no one has ever asked this question before. You'd think
this would come up often; none of my clients have ever used anything but
integrated security. Anyway, I searched everywhere, so if the answer is
previously posted, my abject apologies.
protected void Application_Start(Object sender, EventArgs e)
{
log4net.Util.WindowsSecurityContext log4NetSecurityContext =
new log4net.Util.WindowsSecurityContext();
log4NetSecurityContext.DomainName = "BMT";
log4NetSecurityContext.UserName = "SQL_USER";
log4NetSecurityContext.Password = "abcef";
og4NetSecurityContext.Impersonate(null);
log4NetSecurityContext.ActivateOptions();
XmlConfigurator.Configure();
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
if (hierarchy != null)
{
AdoNetAppender adoAppender =
(AdoNetAppender)hierarchy.Root.GetAppender("SqlServerAppender");
if (adoAppender != null)
{
adoAppender.ConnectionString =
ConfigurationSettings.AppSettings["DefaultConnectionString"];
adoAppender.SecurityContext = log4NetSecurityContext;
adoAppender.ActivateOptions();
}
}
}