如何在WCF中获取自定义用户名/密码
我在消息下的安全模式下使用 clientCredentailType 作为用户名。我创建了一个自定义用户名和密码验证器类来验证用户名和密码。
我想使用这些凭据(用户名、密码)来授权服务中的不同操作。我想知道存储用户名和密码以供重复使用的最佳方法是什么。我是否应该使用自定义验证器类将它们存储在静态变量中,以便可以在任何地方访问它们?
为什么我不能使用 System.Threading.Thread.CurrentPrincipal.Identity.Name,我尝试获取用户名,但它没有向我显示任何内容,如何获取密码?
谢谢 阿德南
I am using clientCredentailType as Username under security mode under Message. I have created a Custom username and password validator class to verify the username and password.
I want to use these credentials (username,password) for authorization to different operations within the service. I want to know whats the best way to store the username and password for reuse. Should I use my custom validator class to store these in static variables so that they are accessible everywhere ?
Why can't I use System.Threading.Thread.CurrentPrincipal.Identity.Name, I tried to get the username but it does not show me anything and how can I get Password ?
Thanks
Adnan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅在自定义验证器中处理和使用凭据。如果凭据有效,请使用自定义授权策略为该用户准备具有一组角色的自定义主体,并使用 PrimaryPermission 属性装饰您的服务方法。
Only process and use the credentials in your custom validator. If the credentials are valid, use a custom authorization policy to prepare a custom principal with a set of roles for that user and decorate your service methods with PrincipalPermission attributes.
线程的 CurrentPrincipal 属性是运行服务的 Windows 标识。您的服务以用户名/密码的形式接收客户端身份,它与服务身份没有关系。 WCF 支持模拟和委托,但不使用像您这样的自定义身份方案服务正在使用。
此问题的可接受答案可能就是您正在寻找的答案。如果这不起作用,那么在代码中手动执行此操作很快就会变得非常难看。
The CurrentPrincipal property of the thread is the Windows identity the service is running under. Your service is receiving the client identity in the form of a user name/password and it has no relationship to the service identity. WCF supports impersonation and delegation but not using a custom identity scheme like your service is using.
The accepted answer to this question may be what you are looking for. If that doesn't work, it gets pretty ugly pretty quickly to do this manually in code.