簡體   English   中英

斷開本地 Windows 用戶使用 C#

[英]Disconnect Local Windows User Using C#

我創建了兩個測試帳戶 test 和 test1。 我可以使用以下代碼登錄到一個本地 windows 用戶帳戶“測試”。

bool result = false;

            ContextType contextType = ContextType.Machine;

            //if (InDomain())
            //{
            //    contextType = ContextType.Domain;
            //}

            try
            {
                using (PrincipalContext principalContext = new PrincipalContext(contextType))
                {
                    result = principalContext.ValidateCredentials(
                        userName,
                        new NetworkCredential(string.Empty, password).Password
                    );
                }
            }
            catch (PrincipalOperationException)
            {
                // Account disabled? Considering as Login failed
                result = false;
            }
            catch (Exception)
            {
                throw;
            }

            return result;

但是,當我嘗試使用另一個帳戶“test1”登錄時,它會拋出以下異常。

Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again

在我看來,之前的用戶“test”已經連接並占用了資源,這就是為什么它不讓用戶“test1”登錄並獲取資源的原因。

我試圖找到一種方法,使第一個用戶“測試”可以斷開連接(或注銷),以便第二個用戶可以登錄。

我曾嘗試通過以下代碼訪問 UserPrinciple 以使連接的用戶斷開連接,但 UserPrinciple 中沒有可用的方法

UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, "test");

必須有某種方法可以注銷(或斷開連接)已登錄的用戶。

我不知道是否有更好的解決方案,但您可以使用net use命令嘗試解決方法。 用手:

  1. 運行net use
  2. 識別相關連接
  3. 為所有相關連接運行net use /d <connection>
  4. 使用新用戶連接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM