获取 Silverlight 中当前的 Windows 用户名
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好吧,以下是如何在不使用 wcf 的情况下获取它
http ://www.codeproject.com/Articles/47520/Silverlight-Windows-User-Identity-Name
well , here is how you can get it wihtout wcf
http://www.codeproject.com/Articles/47520/Silverlight-Windows-User-Identity-Name
// 对 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];
根据这篇文章,它在 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.
我想我应该分享这段似乎有效的代码(YMMV)。 它的灵感来自 CraigTP 的回答和他提供的链接。 我知道这并不能直接回答有关在静态网页中运行的部分,但 OP 似乎接受了 APSX 妥协。
在我托管 Silverlight 的 ASPX 页面中:
在我的 silverlight C# 代码中:
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:
In my silverlight C# code:
这个问题的高票答案对我没有帮助。
使用 ASP.NET Web 服务,这可以工作:
这个博客条目使我走上了正确的道路:
http://rouslan.com/2009/03/12/20-steps-to-get-together-windows -authentication-silverlight-and-wcf-service/
ServiceReferences.ClientConfig 需要这个:
并且 web.config 需要这个:
这些是我需要对以前使用匿名访问的其他工作 Silverlight 应用程序进行的唯一值得注意的更改IIS 中的 Web 服务。
The highly voted answers to this question did not help me.
Using an ASP.NET web service, this worked however:
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:
And web.config needs this:
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.
不幸的是,我认为这是不可能的。
虽然您说我们可以假设 Windows OS/IE,但 Silverlight 本身当然不会假设这一点,因此我们通常可用于获取当前登录用户名的大多数正常 .NET 机制并不存在于子集中Silverlight 应用程序可用的框架:
即。
这些机制在 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.
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.
你可以设法通过这种方式。
1) 创建 ASP.NET Web 服务应用程序。
2) 实现从 silverlight 应用程序调用的 Web 服务和方法。
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.
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.