How to use Windows Authentication in WPF?

后端 未结 2 2001
迷失自我
迷失自我 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.

提交回复
热议问题