如何在 WPF 中使用 Windows 身份验证?

问题描述:

我没有找到太多关于如何在 WPF 应用程序中使用 Windows 身份验证的文档.我不会认为它与任何非 WPF 应用程序有任何不同,但似乎确实如此.我想进入我的项目属性 ->应用程序并确保 Windows 身份验证已启用,但该选项在 WPF 应用程序中不可用(如以下文档所确认).

I'm not finding much documentation on how to use Windows Authentication in a WPF app. I wouldn't have thought that it would be any different than in any non-WPF app, but it seems that it is. I want to go into my project Properties -> Application and ensure that Windows Authentication is on, but that option is not available in a WPF app (as the following document confirms).

http://msdn.microsoft.com/en-us/library/tzdks800.aspx

如果我忽略所有这些而只查看 My.User.Name (VB),它是空的.这告诉我不知何故未启用 Windows 身份验证.

If I ignore all that and just look at My.User.Name (VB), it is empty. This tells me that somehow Windows Authentication is not enabled.

似乎有一个我遗漏的概念;有人能指出我正确的方向吗?

Seems like there is a concept I am missing; could someone point me in the right direction?

我的计划是使用 PrincipalPermissionAttribute 来限制对我应用的某些部分(或者可能是整个应用,通过将其应用于 Application_Startup())的访问.>

My plan is to use PrincipalPermissionAttribute to restrict access to certain parts of my app (or perhaps the entire app, by applying it to Application_Startup()).

Itowlson 的回答是正确的,但是,为了在任何方法上使用 PrincipalPermissionAttribute,您必须首先通过调用使 windows 主体成为当前主体:

Itowlson's answer is correct, but also, in order to use the PrincipalPermissionAttribute on any method, you have to first make the windows principal the current principal by calling:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)