簡體   English   中英

如何在C#.Net 3.5 SP1中顯示身份驗證對話框

[英]How to show authentication dialog in C# .Net 3.5 SP1

我想訪問網絡共享文件。 如何顯示系統身份驗證對話框,以便用戶可以輸入用戶名和密碼?

ps:通過UNC,在WinForm中。

我正在編寫一個瀏覽控件,我想在用戶雙擊網絡共享文件夾后顯示對話框。

不確定我是否理解正確,是否要顯示Windows身份驗證對話框?

嘗試這個:

    /// <summary>
/// Leverages the windows UI to prompt for a password
/// </summary>
internal static class Authentication
{
    public struct CREDUI_INFO
    {
        public int cbSize;
        public IntPtr hwndParent;
        public string pszMessageText;
        public string pszCaptionText;
        public IntPtr hbmBanner;
    }

    [DllImport("credui")]
    private static extern CredUIReturnCodes CredUIPromptForCredentials(ref CREDUI_INFO creditUR,
          string targetName,
          IntPtr reserved1,
          int iError,
          StringBuilder userName,
          int maxUserName,
          StringBuilder password,
          int maxPassword,
          [MarshalAs(UnmanagedType.Bool)] ref bool pfSave,
          CREDUI_FLAGS flags);

    [Flags]
    enum CREDUI_FLAGS
    {
        INCORRECT_PASSWORD = 0x1,
        DO_NOT_PERSIST = 0x2,
        REQUEST_ADMINISTRATOR = 0x4,
        EXCLUDE_CERTIFICATES = 0x8,
        REQUIRE_CERTIFICATE = 0x10,
        SHOW_SAVE_CHECK_BOX = 0x40,
        ALWAYS_SHOW_UI = 0x80,
        REQUIRE_SMARTCARD = 0x100,
        PASSWORD_ONLY_OK = 0x200,
        VALIDATE_USERNAME = 0x400,
        COMPLETE_USERNAME = 0x800,
        PERSIST = 0x1000,
        SERVER_CREDENTIAL = 0x4000,
        EXPECT_CONFIRMATION = 0x20000,
        GENERIC_CREDENTIALS = 0x40000,
        USERNAME_TARGET_CREDENTIALS = 0x80000,
        KEEP_USERNAME = 0x100000,
    }

    public enum CredUIReturnCodes
    {
        NO_ERROR = 0,
        ERROR_CANCELLED = 1223,
        ERROR_NO_SUCH_LOGON_SESSION = 1312,
        ERROR_NOT_FOUND = 1168,
        ERROR_INVALID_ACCOUNT_NAME = 1315,
        ERROR_INSUFFICIENT_BUFFER = 122,
        ERROR_INVALID_PARAMETER = 87,
        ERROR_INVALID_FLAGS = 1004,
    }

    /// <summary>
    /// Prompts for password.
    /// </summary>
    /// <param name="user">The user.</param>
    /// <param name="password">The password.</param>
    /// <returns>True if no errors.</returns>
    internal static bool PromptForPassword(out string user, out string password)
    {
        // Setup the flags and variables
        StringBuilder userPassword = new StringBuilder(), userID = new StringBuilder();
        CREDUI_INFO credUI = new CREDUI_INFO();
        credUI.cbSize = Marshal.SizeOf(credUI);
        bool save = false;
        CREDUI_FLAGS flags = CREDUI_FLAGS.ALWAYS_SHOW_UI | CREDUI_FLAGS.GENERIC_CREDENTIALS;

        // Prompt the user
        CredUIReturnCodes returnCode = CredUIPromptForCredentials(ref credUI, Application.ProductName, IntPtr.Zero, 0, userID, 100, userPassword, 100, ref save, flags);

        user = userID.ToString();
        password = userPassword.ToString();

        return (returnCode == CredUIReturnCodes.NO_ERROR);
    }
}

使用通過此對話框獲得的憑據,您可以按照Phil Harding 此處的說明調用LogonUser。

您當前如何訪問共享? 通過UNC還是首先將其映射到驅動器號? 一種想法是使用帶有CONNECT_INTERACTIVE和CONNECT_PROMPT標志wnetaddconnection2 api調用來映射它。

不確定是在做asp.net還是Win窗體,但是在asp.net中,您可以在web.config中設置身份驗證標簽(對於winforms,則為app.config)

<authentication mode="Windows"/>

    <authorization>
      <allow users="[Users to view that network sare]" />
    </authorization>

要么

<authorization>
  <allow Roles="[Roles to view that network sare]" />
</authorization>

暫無
暫無

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

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