Web 服务不返回当前 Windows 用户名

发布于 2024-08-19 09:25:11 字数 360 浏览 1 评论 0原文

这是我在 Web 服务中用于获取当前 Windows 用户的函数。

 <WebMethod()> _
  Function User() As String
        Dim p() As String = Split(My.User.Name, "\")
        Dim p1 As String = p(1)
        Return p1
    End Function

当我在本地主机上运行服务时,它确实返回当前的 Windows 用户名! 问题是当我从远程电脑运行服务时,在这种情况下我从这个功能中什么也得不到。该服务有什么问题,以及如何获取 Windows 用户的名称?

谢谢!

This is the function that I use in web service for getting current windows user.

 <WebMethod()> _
  Function User() As String
        Dim p() As String = Split(My.User.Name, "\")
        Dim p1 As String = p(1)
        Return p1
    End Function

When I run service on localhost it realy return current windows user name!
The problem is when i run service from remote PC, in that case I got nothing from this function. What is problem with this service, and how I can get name of Windows user?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月寒剑心 2024-08-26 09:25:11

您希望它返回哪个:调用者的身份?或者运行网络服务的服务帐户的身份?

在大多数情况下,windows 身份取决于服务器的配置方式;是否使用模拟?另外,您在呼叫服务时是否传递呼叫者身份?例如,UseDefaultCredentialsCredentials。该服务是否配置为识别用户的声明?

一般来说,我会尽量使用模拟;它需要在服务器上提升,不支持所有环境,并且会对池的有效性产生很大影响 - 所以我不希望 windows 身份流动,但如果你通过某种形式的调用者身份我可能期望“主体”(Thread.CurrentPrincipal)代表调用者。

我不记得来自 asmx,但是使用 WCF,如果需要,您可以编写自己的代码来根据自己的身份验证方案设置主体。

Which do you expect it to return: the identity of the caller? or the identity of the service account that is running the web-service?

In most cases the windows identity would depend on how the server is configured; is it using impersonation? Plus are you passing the callers identity over when calling the service? For example, UseDefaultCredentials or Credentials. And is the service configured to recognise the users claims?

Generally I try not to use impersonation; it requires elevation at the server, doesn't support all environments, and can have a big impact on the effectiveness of pooling - so I wouldn't expect the windows identity to flow, but if you are passing the callers identity in some form I might expect the "principal" (Thread.CurrentPrincipal) to represent the caller.

I can't remember from asmx, but with WCF you can write your own code to setup the principal from your own authentication scheme if you want.

゛清羽墨安 2024-08-26 09:25:11

试试这个

using System.Security.Principal;

[WebMethod()]
public string userName()
{
    return User.Identity.Name.ToString();
}

Try this one

using System.Security.Principal;

[WebMethod()]
public string userName()
{
    return User.Identity.Name.ToString();
}
绮烟 2024-08-26 09:25:11

您可以通过以下代码行获取当前线程正在运行的身份:

System.Threading.Thread.CurrentPrincipal.Identity.Name

You can get the identity the current thread is running under by the below line of code:

System.Threading.Thread.CurrentPrincipal.Identity.Name
深海不蓝 2024-08-26 09:25:11

尝试:

string _userName = WindowsIdentity.GetCurrent().Name;

Try:

string _userName = WindowsIdentity.GetCurrent().Name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文