本文介绍了如何通过设置DataServiceContext.Credentials在自定义DataService上检索已在客户端上设置的凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个自主的DataService(使用类WebServiceHost托管在WPF-Application中)和一个客户端。客户端使用从DataServiceContext继承的代理类(在向我的DataService添加ServiceReference时创建)来访问DataService。I have a selfhosted DataService (hosted inside a WPF-Application using class WebServiceHost) and a client. The client uses a proxy class inherited from DataServiceContext (created during adding a ServiceReference to my DataService) to access the DataService.我希望使用存储在自定义数据库中的用户名/密码来保护DataService。 I want secure the DataService with Username/Password stored in a custom database.如何在服务器上的代码中访问客户端上设置的凭据?How can I access the Credentials set on the client in the code below on the server? 客户代码: Client-Code: var uri = new Uri ( " http:// localhost :6001 / UserManagement" ); dc = new UserManagementService。 UserManagementEntities (uri); dc.Credentials = new NetworkCredential ( " user" , "密码" ); var uri = newUri("http://localhost:6001/UserManagement");dc = new UserManagementService.UserManagementEntities(uri);dc.Credentials = newNetworkCredential("user","Password"); DataService-Code: DataService-Code: public class UserManagementService : DataService < UserManagementEntities > { public 静态 void InitializeService( DataServiceConfiguration config) { // TODO:如何检查凭证!!! // var x = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name; // var s = ServiceSecurityContext.Current.PrimaryIdentity.Name; publicclassUserManagementService : DataService<UserManagementEntities>{ publicstaticvoid InitializeService(DataServiceConfiguration config) { //TODO: Howto check Credentials!!! //var x = OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name; //var s = ServiceSecurityContext.Current.PrimaryIdentity.Name; config。 UseVerboseErrors = config.UseVerboseErrors = true ; config.SetEntitySetAccessRule( " *" , EntitySetRights 。全部); config.SetServiceOperationAccessRule( " *" , ServiceOperationRights .All); //config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; // config.SetEntitySetPageSize(" Users", 5); } } true; config.SetEntitySetAccessRule("*",EntitySetRights.All); config.SetServiceOperationAccessRule("*",ServiceOperationRights.All); //config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; //config.SetEntitySetPageSize("Users", 5); }} 推荐答案 您是如何托管服务的?例如,如果您使用的是WebServiceHost,DataService将需要访问Web Http上下文:How exactly are you hosting the service? For example, if you are using a WebServiceHost, the DataService will need access to the Web Http Context: 使用 系统 。 ServiceModel 。 激活 ; ///<摘要> ///要求WCF主机设置访问当前正在执行的请求的WebHttpContext。 / //更多详情:http://msdn.microsoft.com/en-us/library/aa702682.aspx ///< / summary> [ AspNetCompatibilityRequirements ( RequirementsMode = AspNetCompatibilityRequirementsMode 。 必需 )] public class ContextService : DataService < 背景 > using System.ServiceModel.Activation; /// <summary> /// Require that the WCF host setup access to the WebHttpContext of the currently executing request. /// More details here : http://msdn.microsoft.com/en-us/library/aa702682.aspx /// </summary> [AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Required)] public classContextService : DataService<Context> 然后,你可以访问铜使用HttpContext的rrent用户凭据: if(System.Web.HttpContext.Current.User.Identity.Name = User) 这篇关于如何通过设置DataServiceContext.Credentials在自定义DataService上检索已在客户端上设置的凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 00:08