How do you get credentials (NetworkCredential) of currently logged in user?

前端 未结 6 2094
夕颜
夕颜 2021-02-05 02:01

I\'m writing some code to utilise a 3rd party component, and I need to supply an object which implements ICredentials when I start to use it.

If I write the following...

6条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 02:52

    Have you tried setting the principalpolicy for the appdomain at the start of the application?

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    

    Setting this value before accessing the current principal object via the thread makes sure the windows identity is used in this object.

    edit - I'm pretty sure this works for DefaultNetworkCredentials. I used it to access a web service with windows authentication from a windows forms app.

提交回复
热议问题