ASP.NET Development Server权限和网络资源

时间:2010-09-17 19:39:10

标签: c# .net asp.net vb.net iis

在VS 2010上调试时,我的ASP.NET 4.0 Web App无法访问网络打印机。它可以访问本地打印机。好像它可能是权限问题。由于VS2010调试在ASP.NET Development Server上运行,它必须在我用来登录Windows的帐户下运行,对吧?该用户是否需要作为管理员添加到该打印机用户中?有没有我可以冒充的帐户让这个工作?

3 个答案:

答案 0 :(得分:1)

您是对的,通过Visual Studio进行调试意味着您运行的所有代码都具有与登录到Windows的用户相同的权限。在服务器上,您需要设置模拟和/或设置应用程序池,以便以有权在这些打印机上打印的用户身份运行。

我建议您设置专用域帐户(例如domain \ yourapp-impers-user),并将应用程序池设置为使用该帐户,或在web.config中设置模拟。然后在打印服务器上,您只需为该用户帐户授予必要的权限。

答案 1 :(得分:1)

以下是模拟的示例

    using System;
    using System.Collections.Generic;
    using System.Web.Security;
    using System.Security.Principal;
    using System.Runtime.InteropServices;
    using System.Linq;
    using System.Web;
    using System.IO;

    public partial class Main : PageBase
    {

    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    WindowsImpersonationContext impersonationContext;

    [DllImport("advapi32.dll")]
    public static extern int LogonUserA(String lpszUserName,
        String lpszDomain,
        String lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);
    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {

            if (impersonateValidUser("Username", "Domain", "Password"))
            {
                //Your code under a specific user here.
            }

        }
    }

    private bool impersonateValidUser(String userName, String domain, String password)
    {
        WindowsIdentity tempWindowsIdentity;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                    {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return true;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        return false;
    }

    private void undoImpersonation()
    {
        impersonationContext.Undo();
    }
祝你好运!

答案 2 :(得分:0)

在不确定的情况下,您可以尝试模拟网络服务,或者您自己的域帐户,该帐户应始终有效。