获取 Silverlight 中当前的 Windows 用户名

发布于 2024-07-29 23:25:06 字数 166 浏览 6 评论 0原文

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

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.

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

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

发布评论

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

评论(8

一个人的旅程 2024-08-05 23:25:14

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

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

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

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

二货你真萌 2024-08-05 23:25:13

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

听起来好像没有真正的方法可以做到这一点。

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

sounds like there is no real way to do it.

沫尐诺 2024-08-05 23:25:12
Environment
    .GetFolderPath(Environment.SpecialFolder.Personal)
    .Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)[2];
Environment
    .GetFolderPath(Environment.SpecialFolder.Personal)
    .Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)[2];
浅听莫相离 2024-08-05 23:25:11

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

在我托管 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>

在我的 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");
 }

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.

In my ASPX page that hosts Silverlight:

<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:

 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");
 }
策马西风 2024-08-05 23:25:11

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

使用 ASP.NET Web 服务,这可以工作:

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

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

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

ServiceReferences.ClientConfig 需要这个:

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

并且 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>

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

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

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

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/

ServiceReferences.ClientConfig needs this:

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

And web.config needs this:

  <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.

昔梦 2024-08-05 23:25:10

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

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

即。

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

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

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

顺便说一句,这个问题也在这里被问到:

当前 Windows 用户名和域

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

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:

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.

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.

Incidentally, this question was also asked over here:

Current Windows Username and Domain

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 not the Windows username of currently logged on user of the client machine.

嘴硬脾气大 2024-08-05 23:25:09

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

1) 创建 ASP.NET Web 服务应用程序。

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

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

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

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

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

You can manage to get by this way.

1) Create asp.net web service application.

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

[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.

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

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

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