How to save user object in WCF

风流意气都作罢 提交于 2019-12-05 07:08:12

The short answer is that the most correct option is to create an IPrincipal instance that represents the authenticated user and put it on Thread.CurrentPrincipal.

All code running on the same thread later in the service will be able to access the authenticated user through Thread.CurrentPrincipal.

This is the standard way of dealing with authenticated users in .NET (and therefore also in WCF).

It isn't that simple. The problem is that the custom UserNamePasswordValidator gets called before the AuthorizationPolicy.Evaluate() and somewhere in between, WCF initializes Thread.CurrentPrincipal itself. I've tried setting it inside the password validator but it doesn't work, it gets clobbered immediately after by WCF when the initial principal is created. The only way I can make it work is if I wait and set Thread.CurrentPrincipal in the AuthorizationPolicy.Evaluate() method.

I would like to hear a WCF guru explain this because I see so many WCF beginners having the same issue with this and I see it as a flaw in the WCF design.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!