Form Authentication on NetTcpBinding in WCF service
-
06-07-2021 - |
Вопрос
I am using WCF service, which have two endpoint WsHttpBinding and NetTcpBinding and the service is using Forms Authentication. Service is hosted on IIS 7.
This works perfectly with WsHttpBinding, but fails for NetTcpBinding.
It fails on below statement:
FormsAuthentication.SetAuthCookie("COOKIENAME", false);
And the exception is :
Object reference not set to an instance of an object.
Please share your ideas on this.
Решение
Forms Authentication requires cookies/session which is not supported by protocol itself. So, Forms authentication can not implemented on NetTcpBinding of WCF service.
Другие советы
Option 1:
- Add references to
System.IdentityModel
&System.IdentityModel.Selectors
as well as the WCF assemblies. - Set the security mode to Message on your binding
- Set the
Message.ClientCredentialType
toMessageCredentialType.UserName
- Create a type derived from
UserNamePasswordValidator
and implement the only method. You should throw a SecurityTokenException if the user name / password pair does not validate.
On your service host instance's Credentials property, set:
UserNameAuthentication.UserNamePasswordValidationMode
toUserNamePasswordValidationMode.Custom
UserNameAuthentication.CustomUserNamePasswordValidator
to a new instance of yourUserNamePasswordValidator
-derived class.- Set a service certificate with
ServiceCertificate.SetCertificate()
As for the client-side credentials dialog, you can either make one yourself and on your proxy set proxy.ClientCredentials.UserName.UserName
& proxy.ClientCredentials.UserName.Password
before you open the proxy / use it the first time. Or you can check out how you can implement the System.ServiceModel.Dispatcher.IInteractiveChannelInitializer
to create your own interactive initialization UI.
Option 2:
Another Alternative this sounds more like what you want to do ..Passing FormsAuthentication cookie to a WCF service
Why did I provide an answer to an old post - because someone might be looking for an answer. Hope this helps.