Here i'm using
1) WCF service(Windows Authenticated and Impersonation)
2) Sql server(Windows Authentication)
3) Console Application(Client)
All Are in Same Domain
I'm consuming windows authenticated WCF service from console Application(Client) so when i'm trying to access SQL server from console application using WCF service it's showing me error :
Exception In Account Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
My scenario is
my WCF service and SQL server is in one system (Suppose system A) and my console application (My Client) is in another system(Suppose System B).
when my client(System B) send request it hit my service successfully but when service try to access SQL server it shows error:
Exception In Account Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
What I have tried:
MY Client Side Code:
static void Main(string[] args)
{
try
{
ServiceClient obj = new ServiceClient();
obj.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
obj.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
AccModel objAccModel = new AccModel();
objAccModel.ACC_No = "jkh90";
objAccModel.Remark = "dsdsadasd";
objAccModel.ACC_Code = "AA";
var strflight = obj.Acc_Validate(objAccModel);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
MY Server Side Code(WCF Service)
public string Acc_Validate(ACCModel objACCModel)
{
try
{
using (WindowsImpersonationContext ctx = WindowsIdentity.GetCurrent().Impersonate())
{
SqlParameter[] parameter = {
new SqlParameter("@CMD","ACCVALIDATE"),
new SqlParameter("@ACC_No",objACCModel.ACC_No),
new SqlParameter("@ACC_Code",objACCModel.ACC_Code),
new SqlParameter("@PNRIDOUT", SqlDbType.NVarChar,800) {Direction = ParameterDirection.Output}
};
strStatus = SQLHelper.ExecuteNonQueryTwoOutputResult(SQLHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, DBConstants.PRIU_ACC_DETAILS, "@PNRIDOUT", parameter);
}
}
catch (Exception ex)
{
ClsLogging.writefile("Errorrrrrrrrrrr " + ex.Message, ClsLogging.LogType.BTA_Service_Exception);
}
}
WCF Service(Web Config File)
="1.0"
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<pages controlRenderingCompatibilityVersion="4.0"/>
<httpCookies httpOnlyCookies="true"/>
<authentication mode="Windows">
<identity impersonate="true"/>
<connectionstrings>
<add name="Connection" connectionString="Initial Catalog=ACCDB;Data Source=*****;Integrated Security=True;"/>
<system.servicemodel>
<services>
<service name="Service.Service" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="TransportSecurity" contract="Service.IService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<bindings>
<basichttpbinding>
<binding name="TransportSecurity">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
<behaviors >
<servicebehaviors>
<behavior name="ServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceMetadata httpGetEnabled="true"/>
<serviceAuthorization impersonateCallerForAllOperations="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceSecurityAudit auditLogLocation="Application" suppressAuditFailure="false" serviceAuthorizationAuditLevel="SuccessOrFailure" messageAuthenticationAuditLevel="SuccessOrFailure"/>
<serviceThrottling maxConcurrentCalls="500" maxConcurrentInstances="100" maxConcurrentSessions="200"/>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false"/>
<system.webserver>
<validation validateIntegratedModeConfiguration="false" />
<httpprotocol>
<customheaders>
<add name="X-Content-Type-Options" value="nosniff"/>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>