在 Silverlight 中获取当前的 Windows 用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1176566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:36:17  来源:igfitidea点击:

Get current Windows user name within Silverlight

windowssilverlightkerberosntlm

提问by huseyint

Is it possible to get the currently logged in user's username with Silverlight? You can assume that user has Windows OS and the Silverlight application is hosted in Internet Explorer. Getting the identity from server side with ASP.NET is not an option, this Silverlight application will be hosted on a static HTML file.

是否可以使用 Silverlight 获取当前登录用户的用户名?您可以假设用户拥有 Windows 操作系统并且 Silverlight 应用程序托管在 Internet Explorer 中。使用 ASP.NET 从服务器端获取身份不是一种选择,此 Silverlight 应用程序将托管在静态 HTML 文件上。

回答by Richard

You can manage to get by this way.

你可以设法通过这种方式获得。

1) Create asp.net web service application.

1) 创建asp.net Web 服务应用程序。

2) Implement web service and method to call from silverlight applicaton.

2) 实现从 Silverlight 应用程序调用的 Web 服务和方法。

[WebMethod]
public string GetClientUserName()
{
    return System.Web.HttpContext.Current.User.Identity.Name.ToString();
}

3) Deploy this web service application on web server. Don't allow anonymous user to access this.

3) 在 Web 服务器上部署此 Web 服务应用程序。不允许匿名用户访问它。

4) Add this service to Silverlight application. (Add service reference)

4) 将此服务添加到 Silverlight 应用程序。(添加服务参考)

5) Now, you can call this method and get user name from entire silverlight application.

5) 现在,您可以调用此方法并从整个 Silverlight 应用程序中获取用户名。

回答by CraigTP

Unfortunately, I don't think it's possible.

不幸的是,我认为这是不可能的。

Although you say that we can assume Windows OS/IE, Silverlight itself certainly doesn't assume this, so most of the normal .NET mechanisms that would ordinarily be available to us to get the current logged on user's name do not exist within the subset of the framework available to Silverlight apps:

尽管您说我们可以假设 Windows 操作系统/IE,但 Silverlight 本身当然不会假设这一点,因此通常可用于获取当前登录用户名的大多数正常 .NET 机制不存在于子集中Silverlight 应用程序可用的框架:

ie.

IE。

System.Net.CredentialCache.DefaultCredentials  
System.Security.Principal.WindowsIdentity.GetCurrent().Name  
Environment.UserName  

are all unavailable within a Silverlight application, whereas in (say) a Windows Forms Application, each of these mechanisms is available to use.

在 Silverlight 应用程序中全部不可用,而在(比如)Windows 窗体应用程序中,这些机制中的每一个都可以使用。

I suppose it makes sense, really, since there's no guarantee that the Silverlight application is going to be running on top of a Windows/IE platform.

我想这是有道理的,真的,因为不能保证 Silverlight 应用程序将在 Windows/IE 平台上运行。

Incidentally, this question was also asked over here:

顺便提一下,这里也有人问过这个问题:

Current Windows Username and Domain

当前 Windows 用户名和域

and that thread seems to confirm that there's no native way to achieve this. The thread then goes on to suggest "injecting" the current ASP.NET user name from the ASP.NET page "hosting" the Silverlight app. into the Silverlight application itself prior to it running in the context of the client's machine. Of course, even if this works, it'll only give you the ASP.NET user name from either ASP.NET's forms or windows based authentication and notthe Windows username of currently logged on user of the client machine.

并且该线程似乎确认没有本地方法可以实现此目的。然后该线程继续建议从“托管”Silverlight 应用程序的 ASP.NET 页面“注入”当前的 ASP.NET 用户名。在 Silverlight 应用程序在客户端机器的上下文中运行之前进入它本身。当然,即使这可行,它也只会为您提供来自 ASP.NET 表单或基于 Windows 的身份验证的 ASP.NET 用户名,而不是客户端计算机当前登录用户的 Windows 用户名。

回答by Michael Maddox

The highly voted answers to this question did not help me.

这个问题的高票答案对我没有帮助。

Using an ASP.NET web service, this worked however:

然而,使用 ASP.NET Web 服务,这有效:

string userName =
   System.ServiceModel.ServiceSecurityContext.Current.WindowsIdentity.Name;

And this blog entry is the one that set me on the correct path:

这个博客条目让我走上了正确的道路:

http://rouslan.com/2009/03/12/20-steps-to-get-together-windows-authentication-silverlight-and-wcf-service/

http://rouslan.com/2009/03/12/20-steps-to-get-together-windows-authentication-silverlight-and-wcf-service/

ServiceReferences.ClientConfig needs this:

ServiceReferences.ClientConfig 需要这个:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding ...>
          <security mode="TransportCredentialOnly" />
        </binding>
      </basicHttpBinding>
    </bindings>
 </system.serviceModel>

And web.config needs this:

而 web.config 需要这个:

  <system.web>
    <authentication mode="Windows" />
    <identity impersonate="false" />
  </system.web>

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding ...>
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>

Those are the only noteworthy changes I needed to make to an otherwise working Silverlight application that previously used anonymous access for web services in IIS.

这些是我需要对以前使用匿名访问 IIS 中的 Web 服务的 Silverlight 应用程序进行的唯一值得注意的更改。

回答by Aardvark

I figured I'd share this code which seems to work (YMMV). It is inspired by CraigTP's answer and his links he provided. I know this doesn't directly answer the part about running in a static web page, but it seems the OP accepted the APSX compromise.

我想我会分享这个似乎有效的代码(YMMV)。它的灵感来自 CraigTP 的回答和他提供的链接。我知道这并没有直接回答关于在静态网页中运行的部分,但似乎 OP 接受了 APSX 妥协。

In my ASPX page that hosts Silverlight:

在我托管 Silverlight 的 ASPX 页面中:

<head>
    <!-- ...snip... -->
    <script runat="server" language="c#">
    void Page_Load()
    {
        this.UsernameField.Value = User.Identity.Name;
    }        
    </script>
</head>
<body>
   <input type="hidden" ID="UsernameField" runat="server" />
   <!-- ...snip... -->
</body>

In my silverlight C# code:

在我的 Silverlight C# 代码中:

 private string GetCurrentUserName()
 {
     HtmlDocument doc = HtmlPage.Document;

     if (doc == null)
     {
         return string.Empty;
     }

     HtmlElement elm = doc.GetElementById("UsernameField");

     if (elm == null)
     {
         return string.Empty;
     }

     return elm.GetAttribute("value");
 }

回答by ywm

Environment
    .GetFolderPath(Environment.SpecialFolder.Personal)
    .Split(new[] { '\' }, StringSplitOptions.RemoveEmptyEntries)[2];

回答by technocrusaders.com

// a small improvement of Code Maverick answer using System.IO.Path.DirectorySeparatorChar rather // then //

// 使用 System.IO.Path.DirectorySeparatorChar 而不是 Code Maverick 答案的小改进 // 然后 //

Environment .GetFolderPath(Environment.SpecialFolder.Personal) .Split(new[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)[2];

环境 .GetFolderPath(Environment.SpecialFolder.Personal) .Split(new[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)[2];

回答by Zakos

well , here is how you can get it wihtout wcf

好吧,这里是你如何在没有 wcf 的情况下获得它

http://www.codeproject.com/Articles/47520/Silverlight-Windows-User-Identity-Name

http://www.codeproject.com/Articles/47520/Silverlight-Windows-User-Identity-Name

回答by SmartyP

according to this post its not possible in javascript, so i think your out of luck: http://bytes.com/topic/javascript/answers/616501-how-get-windows-username-using-javascripts

根据这篇文章,它在 javascript 中是不可能的,所以我认为你不走运:http: //bytes.com/topic/javascript/answers/616501-how-get-windows-username-using-javascripts

sounds like there is no real way to do it.

听起来没有真正的方法可以做到。