如何连接到网络共享时提供的用户名和密码 [英] How to provide user name and password when connecting to a network share

查看:636
本文介绍了如何连接到网络共享时提供的用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当连接到网络共享为当前用户(在我的情况下,网络支持服务的用户)没有权利,名称和密码必须提供。

When connecting to a network share for which the current user (in my case, a network enabled service user) has no rights, name and password have to be provided.

我知道如何与Win32函数,但是(从 MPR.DLL WNET * 家庭)做到这一点愿与.net(2.0)功能来做到这一点。

I know how to do this with Win32 functions (the WNet* family from mpr.dll), but would like to do it with .Net (2.0) functionality.

什么选择?

也许一些更多的信息可以帮助:


  • 用例是一个Windows服务,而不是Asp.Net应用程序。

  • 该服务是这对份额没有权限的帐户下运行。

  • 所需共享的用户帐户是不知道在客户端。

  • 客户端和服务器不在同一个域的成员。

推荐答案

您可以更改线程的身份,或P / Invoke的WNetAddConnection2。我preFER后者,因为我有时需要维护不同位置的多个凭据。我把它包装成一个IDisposable接口,并呼吁WNetCancelConnection2事后删除vim的信任状(避免了多个用户名错误):

You can either change the thread identity, or P/Invoke WNetAddConnection2. I prefer the latter, as I sometimes need to maintain multiple credentials for different locations. I wrap it into an IDisposable and call WNetCancelConnection2 to remove the creds afterwards (avoiding the multiple usernames error):

using (new NetworkConnection(@"\\server\read", readCredentials))
using (new NetworkConnection(@"\\server2\write", writeCredentials)) {
   File.Copy(@"\\server\read\file", @"\\server2\write\file");
}

这篇关于如何连接到网络共享时提供的用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆