How to use Windows Authentication in WPF?

后端 未结 2 1997
迷失自我
迷失自我 2021-02-01 10:54

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 s

相关标签:
2条回答
  • 2021-02-01 11:20

    The reason this doesn't work in WPF is that these services are implemented in VB's WindowsFormsApplicationBase class, which isn't used in WPF applications. To do the same thing yourself:

    Call WindowsIdentity.GetCurrent() to get the Windows user identity. You can get the name from this.

    If you specifically want to set the thread principal the way the VB Windows Authentication option does, call Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()) -- this is exactly what WindowsFormsApplicationBase does internally.

    EDIT: If you prefer the My.User API, it looks like you should be able to do the same thing by calling My.User.InitializeWithWindowsUser(). I haven't tested this though.

    0 讨论(0)
  • 2021-02-01 11:25

    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)
    
    0 讨论(0)
提交回复
热议问题