最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 使用特定帳號(hào)密碼訪問Windows網(wǎng)路共享

 更新時(shí)間:2021年03月04日 09:28:14   作者:黑暗執(zhí)行緒  
這篇文章主要介紹了c# 使用特定帳號(hào)密碼訪問Windows網(wǎng)路共享的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

透過程式存取Windows網(wǎng)路分享的檔案也算常見需求,但存取身分是個(gè)問題。之前我慣用的技巧是用有權(quán)限的AD網(wǎng)域帳號(hào)執(zhí)行排程存取網(wǎng)路分享,但這招要搬進(jìn)網(wǎng)站或遇到不同網(wǎng)路分享用不同帳號(hào)便會(huì)破功。最近遇上類似議題,直覺要得回頭靠WinAPI Impersonation解決,之前曾寫過通用元件,擔(dān)心11年前Windows Vista/7時(shí)代的作品有點(diǎn)過時(shí),就爬文找找更好的做法。

之前的變身做法是改變Thread的執(zhí)行身分,然而針對(duì)網(wǎng)路分享還有另一個(gè)WinAPI - WNetAddConnection2,可做到對(duì)多個(gè)網(wǎng)路分享使用不同登入身分。在stackoverflow找到有人分享把它包成搭配using使用的Context物件,符合我慣用的風(fēng)格,二話不說,按贊并寫文分享。

為方便使用,我再做了一層包裝,寫了一個(gè)NetworkCopier,將查目錄或復(fù)制檔案動(dòng)作簡(jiǎn)化成Copy(srcNetPath, targetPath, domain, userId, passwd)、DirFiles(srcNetPath, domain, userId, passwd),并支援預(yù)設(shè)帳密可省略輸入domain, userId, passwd;另外還有GetConnetionContext(path, domain, userId, passwd) 可取回NetworkConnection 物件方便用同一連線身分進(jìn)行多項(xiàng)操作,若來源與目的屬不同網(wǎng)路分享則可套疊多重身分,寫成:

using (var ctxA = NetworkCopier.GetConnetionContext("\\SvrA\Share", MyAD", "userX", "***")
{
 using (var ctxB = NetworkCopier.GetConnetionContext("\\SvrB\Share", MyAD", "userY", "***") 
 {
  File.Copy(@"\\SvrA\Share\SubFolder\test.txt", @"\\SvrB\Share\test.txt");
  File.Copy(@"\\SvrA\Share\hello.txt", @"\\SvrB\Share\Temp\hello.txt");
 }
}

建立NetworkConnection時(shí),需要引入共享路徑而不是完整路徑,例如要訪問\\SvrA\Share\SubFolder\test.txt,建立NetworkConnection的參數(shù)為\\SvrA\Share\,為省去人工截取的麻煩,我寫了一段正則表達(dá)式更順手。

完整程式如下,需要的朋友請(qǐng)自取使用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Web;

namespace MyApp.Models
{
 public class NetworkCopier
 {
  static string defaultDomain = null;
  static string defaultUserId = null;
  static string defaultPasswd = null;
  static NetworkCopier()
  {
   try
   {
    //TODO: 由設(shè)定檔、Registry 或 DB 取得帳號(hào)設(shè)定,密碼記得要加密保存
    var p = ReadCredentialInfo().Split('\t');
    defaultDomain = p[0];
    defaultUserId = p[1];
    defaultPasswd = p[2];
   }
   catch { }
  }
  static string NotNull(string s)
  {
   if (string.IsNullOrEmpty(s)) 
    throw new ApplicationException("未設(shè)定預(yù)設(shè)登入身分");
   return s;
  }
  static string DefaultDomain => NotNull(defaultDomain);
  static string DefaultUserId => NotNull(defaultUserId);
  static string DefaultPassword => NotNull(defaultPasswd);
  static string GetSharePath(string path)
  {
   var m = Regex.Match(path, @"^\\\\[^\\]+\\[^\\]+");
   if (m.Success) return m.Value;
   return path;
  }
  public static void Copy(string srcPath, string dstPath, string domain = null, string userId = null, string passwd = null)
  {
   using (new NetworkConnection(GetSharePath(srcPath), 
    new NetworkCredential(userId ?? DefaultUserId, passwd ?? DefaultPassword, domain ?? DefaultDomain)))
   {
    File.Copy(srcPath, dstPath);
   }
  }
  public static string[] DirFiles(string path, string pattern, string domain = null, string userId = null, string passwd = null)
  {
   using (new NetworkConnection(GetSharePath(path), 
    new NetworkCredential(userId ?? DefaultUserId, passwd ?? DefaultPassword, domain ?? DefaultDomain)))
   {
    return Directory.GetFiles(path, pattern);
   }
  }
  
  public static GetConnectionContext(string path, string domain = null, string userId = null, string passwd = null) 
  {
   return new NetworkConnection(GetSharePath(path), 
    new NetworkCredential(userId ?? DefaultUserId, passwd ?? DefaultPassword, domain ?? DefaultDomain));
  }
  
 }
 //引用來源: https://stackoverflow.com/a/1197430/288936
 public class NetworkConnection : IDisposable
 {
  string _networkName;
  public NetworkConnection(string networkName, NetworkCredential credentials)
  {
   _networkName = networkName;
   var netResource = new NetResource()
   {
    Scope = ResourceScope.GlobalNetwork,
    ResourceType = ResourceType.Disk,
    DisplayType = ResourceDisplaytype.Share,
    RemoteName = networkName
   };
   var userName = string.IsNullOrEmpty(credentials.Domain)
    ? credentials.UserName
    : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
   var result = WNetAddConnection2(
    netResource,
    credentials.Password,
    userName,
    0);
   if (result != 0)
   {
    throw new Win32Exception(result);
   }
  }
  ~NetworkConnection()
  {
   Dispose(false);
  }
  public void Dispose()
  {
   Dispose(true);
   GC.SuppressFinalize(this);
  }
  protected virtual void Dispose(bool disposing)
  {
   WNetCancelConnection2(_networkName, 0, true);
  }
  [DllImport("mpr.dll")]
  private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
  [DllImport("mpr.dll")]
  private static extern int WNetCancelConnection2(string name, int flags, bool force);
 }
 [StructLayout(LayoutKind.Sequential)]
 public class NetResource
 {
  public ResourceScope Scope;
  public ResourceType ResourceType;
  public ResourceDisplaytype DisplayType;
  public int Usage;
  public string LocalName;
  public string RemoteName;
  public string Comment;
  public string Provider;
 }
 public enum ResourceScope : int
 {
  Connected = 1,
  GlobalNetwork,
  Remembered,
  Recent,
  Context
 };
 public enum ResourceType : int
 {
  Any = 0,
  Disk = 1,
  Print = 2,
  Reserved = 8,
 }
 public enum ResourceDisplaytype : int
 {
  Generic = 0x0,
  Domain = 0x01,
  Server = 0x02,
  Share = 0x03,
  File = 0x04,
  Group = 0x05,
  Network = 0x06,
  Root = 0x07,
  Shareadmin = 0x08,
  Directory = 0x09,
  Tree = 0x0a,
  Ndscontainer = 0x0b
 }
}

以上就是c# 使用特定帳號(hào)密碼訪問Windows網(wǎng)路共享的詳細(xì)內(nèi)容,更多關(guān)于c# 訪問Windows網(wǎng)路共享的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 介紹C# 泛型類在使用中約束

    介紹C# 泛型類在使用中約束

    這篇文章介紹了C# 泛型類在使用中約束,有需要的朋友可以參考一下
    2013-09-09
  • Mongodb在CSharp里實(shí)現(xiàn)Aggregate實(shí)例

    Mongodb在CSharp里實(shí)現(xiàn)Aggregate實(shí)例

    本篇文章主要介紹了Mongodb在CSharp里實(shí)現(xiàn)Aggregate實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • C#使用哈希表實(shí)現(xiàn)XML文件查詢

    C#使用哈希表實(shí)現(xiàn)XML文件查詢

    這篇文章主要為大家詳細(xì)介紹了C#如何使用哈希表實(shí)現(xiàn)XML文件查詢功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下
    2024-02-02
  • C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印?

    C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印?

    這篇文章主要介紹了C#/VB.NET實(shí)現(xiàn)在 Word 中插入水印,水印是指在 Word 文檔的背景中以淡色或灰色顯示的文本或圖像。文章圍繞主題展開介紹,需要的朋友可以參考一下
    2022-08-08
  • c# 計(jì)算時(shí)間間隔的簡(jiǎn)單方法(推薦)

    c# 計(jì)算時(shí)間間隔的簡(jiǎn)單方法(推薦)

    下面小編就為大家?guī)硪黄猚# 計(jì)算時(shí)間間隔的簡(jiǎn)單方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • C#用NPOI導(dǎo)出導(dǎo)入Excel幫助類

    C#用NPOI導(dǎo)出導(dǎo)入Excel幫助類

    這篇文章主要為大家詳細(xì)介紹了C#?NPOI導(dǎo)出導(dǎo)入Excel幫助類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C# 使用 OleDbConnection 連接讀取Excel的方法

    C# 使用 OleDbConnection 連接讀取Excel的方法

    這篇文章主要介紹了C# 使用 OleDbConnection 連接讀取Excel的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Unity實(shí)現(xiàn)識(shí)別圖像中主體及其位置

    Unity實(shí)現(xiàn)識(shí)別圖像中主體及其位置

    EasyDL基于飛槳開源深度學(xué)習(xí)平臺(tái),面向企業(yè)AI應(yīng)用開發(fā)者提供零門檻AI開發(fā)平臺(tái),實(shí)現(xiàn)零算法基礎(chǔ)定制高精度AI模型。本文將利用Unity和EasyDL實(shí)現(xiàn)識(shí)別圖像中主體及其位置,感興趣的可以了解一下
    2022-02-02
  • C#使用AutoMapper進(jìn)行對(duì)象映射的示例代碼

    C#使用AutoMapper進(jìn)行對(duì)象映射的示例代碼

    AutoMapper 是一個(gè)對(duì)象到對(duì)象映射的庫(kù),可以簡(jiǎn)化 DTO (Data Transfer Objects) 和實(shí)體類之間的轉(zhuǎn)換,在大型應(yīng)用程序中,通常需要將業(yè)務(wù)實(shí)體映射到視圖模型或 DTO 中,本文將詳細(xì)介紹如何在 C# 項(xiàng)目中使用 AutoMapper,包括安裝、配置、以及示例代碼
    2024-08-08
  • C#面向切面編程之AspectCore用法詳解

    C#面向切面編程之AspectCore用法詳解

    AspectCore?是Lemon名下的一個(gè)國(guó)產(chǎn)Aop框架,提供了一個(gè)全新的輕量級(jí)和模塊化的Aop解決方案,下面我們就來深入了解下AspectCore在C#中的具體使用吧
    2024-01-01

最新評(píng)論

鹿邑县| 勐海县| 栖霞市| 喀什市| 沁水县| 张掖市| 错那县| 蓝田县| 会宁县| 岑巩县| 三明市| 南部县| 卢湾区| 苍梧县| 视频| 涡阳县| 青铜峡市| 大悟县| 柏乡县| 津南区| 丰县| 九江市| 八宿县| 达拉特旗| 扎鲁特旗| 岫岩| 万源市| 安福县| 星子县| 隆尧县| 广西| 万载县| 墨玉县| 崇明县| 保德县| 田东县| 米易县| 谢通门县| 赤城县| 垣曲县| 六安市|