通过 Web 服务调用的类中的 C# 私有变量不起作用?
我试图找出哪个用户正在调用我的网络服务。为此,我想传递Environment.UserName。但是,如果它只是一个字符串,那么任何编写自己的客户端的人都可以输入“asdf”并绕过我想要做的事情。因此,在我的 Web 服务中声明了一个公共类:
public class UserInfo
{
private string userName;
public UserInfo()
{
userName = Environment.UserDomainName + "/" + Environment.UserName + " on " + Environment.MachineName;
}
public string getUserName()
{
return userName;
}
}
调用:时
ClientTest.ServiceReference.UserInfo user = new ClientTest.ServiceReference.UserInfo();
现在,当我在客户端中 ,我认为它会获取我的信息(“MyComputer 上的域/我”)。事实并非如此。 Web 服务将获取其信息(“ServerComputer 上的 NT AUTHORITY/NETWORK SERVICE”)
是否有办法修改我的代码以使其正常工作?我的解决方案可以做到这一点吗?有更好的办法吗?
I am trying to figure out what user is calling my web service. To do this, I would like to pass in the Environment.UserName. However, if its just a string, then anyone writing their own client can just put in "asdf" and bypass what I am trying to do. So declared a public class in my Web Service:
public class UserInfo
{
private string userName;
public UserInfo()
{
userName = Environment.UserDomainName + "/" + Environment.UserName + " on " + Environment.MachineName;
}
public string getUserName()
{
return userName;
}
}
Now when I call:
ClientTest.ServiceReference.UserInfo user = new ClientTest.ServiceReference.UserInfo();
in my client, I thought it would get my information ("Domain/Me on MyComputer"). It does not. The web service will gets its information ("NT AUTHORITY/NETWORK SERVICE on ServerComputer")
Is there anyway to revise my code to get this to work? Is this even possible with my solution? Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为环境是你想要使用的。看起来您所做的只是获取运行服务的域、用户名和计算机,而不是尝试访问该服务的用户的域、用户名和计算机。
我相信您想要的是 WebService.Context 对象。这使您可以访问有关 HTTP 客户端的信息。
I don't think Environment is what you want to be using. It looks like all you are doing is getting the domain, username and machine that the service is running under, not of the user trying to access the service.
I believe what you want is the WebService.Context object. This gives you access to information regarding the HTTP client.