代码之家  ›  专栏  ›  技术社区  ›  SharePoint Newbie

需要模拟用户以访问网络资源、ASP.NET帐户

  •  3
  • SharePoint Newbie  · 技术社区  · 16 年前

    我需要访问一个只有给定域帐户可以访问的网络资源。 我正在使用LogonUser调用,但获取“用户没有必需的priviliege”异常,因为Web应用程序正在使用ASP.NET帐户运行,并且它没有足够的权限进行此调用。

    有办法绕过它吗? 更改ASP.NET帐户的标识或权限不是一个选项,因为这是一台运行了许多项目的生产计算机。 有没有更好的方法来实现这一点?

    使用ASP.NET 2.0进行窗体身份验证。

    亲切的问候。

    5 回复  |  直到 15 年前
        1
  •  7
  •   Michael L Perry    16 年前

    仅仅调用LogonUser是不够的。您需要模拟该用户。您可以模拟以仅访问网络资源。

    示例代码位于 MSDN .

        2
  •  1
  •   Eoin Campbell    16 年前

    您可以添加一个

    <identity impersonate="true" userName=""/>
    

    标记到web.config,但这可能不理想,因为您可能不想以该用户的身份运行整个站点…

    能否将网络共享映射为具有域名和密码的本地驱动器…然后通过映射的驱动器将文件拉到网站上?

    NET USE Z: \\SERVER\Share password /USER:DOMAIN\Username /PERSISTENT:YES
    
        3
  •  0
  •   Dan F    16 年前

    我只在1.1以下有过亲密的经历,所以在2.0天内事情可能会发生变化,但是… 我们有一个应用程序可以部署在内部网场景中,我们也会遇到同样的情况。我们在身份模拟打开、表单模式身份验证和匿名访问关闭的情况下运行。控制这个(我找到的)最简单的方法是将有访问权的用户的凭证放在web.config中。它们在您打开身份模拟的节点上运行。如果是超级屏幕信息,我不会这样做的!我们只在打印环境中访问共享的图形,因此大多数网站都乐于为我们设置一个有限的帐户,以便放入web.confit。 logonuser确实需要提升权限。关于如何在代码中模拟特定用户,msdn有一些很好的文章。我想找出一些链接,但这个电话不做复制粘贴。

        4
  •  0
  •   Howard Pinsley    16 年前

    您可以更改保护网络资源的ACL吗?我过去使用的一个技巧是创建一个Active Directory组,然后将计算机对象放入该组。然后,在我需要访问的对象(文件、共享等)的访问控制列表中使用该组。

    这允许我作为本地系统运行Windows服务并访问受保护的网络资源。这个技巧似乎也适用于作为网络服务运行的ASP.NET进程。

        5
  •  0
  •   Urko    15 年前
    • 使用此Web部件Y连接到具有受限访问权限的网络资源时,我放置了一个文件并Y关闭与该资源的连接(作为具有授权访问权限的用户),您不需要创建一个新的共享连接,这是我的Sistems Department对我所做的唯一限制。也许,有很多导入是必要的,但是我做了很多测试,我没有时间清理代码。希望对你有帮助。(为我糟糕的英语感到抱歉)。

    进口制度 导入System.ComponentModel 导入system.web.ui 导入system.web.ui.webcontrols 导入system.io 导入system.io.file 导入系统。诊断 导入system.xml.serialization 导入Microsoft.SharePoint 导入Microsoft.SharePoint.utilities 导入Microsoft.SharePoint.WebPartPages 导入Microsoft.SharePoint.WebControls 导入Microsoft.SharePoint.Administration 导入system.security.principal 导入system.security.permissions 导入System.Runtime.InteropServices 导入系统环境 导入system.net.sockets 导入system.web.ui.htmlcontrols

    公共阶级非个人主义 private const logon32_provider_默认为integer=0 private const logon32_logon_interactive as integer=2

    <DllImport("advapi32.dll", SetLastError:=True)> _
    Public Shared Function LogonUser(ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Boolean
    End Function
    
    <DllImport("advapi32.dll", EntryPoint:="DuplicateToken", ExactSpelling:=False, CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function DuplicateToken(ByVal ExistingTokenHandle As IntPtr, ByVal ImpersonationLevel As Integer, ByRef DuplicateTokenHandle As IntPtr) As Integer
    End Function
    
    Public Shared Function WinLogOn(ByVal strUsuario As String, ByVal strClave As String, ByVal strDominio As String) As WindowsImpersonationContext
        Dim tokenDuplicate As New IntPtr(0)
        Dim tokenHandle As New IntPtr(0)
        If LogonUser(strUsuario, strDominio, strClave, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle) Then
            If DuplicateToken(tokenHandle, 2, tokenDuplicate) <> 0 Then
                Return (New WindowsIdentity(tokenDuplicate)).Impersonate()
            End If
        End If
        Return Nothing
    End Function
    

    端类 'WebPart1的说明。 “”,xmlRoot(命名空间:=“spScopearFicHero”)>。_ 公共类Web部件1 继承Microsoft.SharePoint.WebPartPages.WebPart

    Protected WithEvents File1 As HtmlInputFile
    
    Dim vdestino As String = "\\centappd20nd01\uploads_avisos"
    Dim vtemporal As String = "c:\pdf"
    
    Protected WithEvents boton1 As Button
    Protected WithEvents usuario As TextBox
    Protected WithEvents contra As TextBox
    Protected WithEvents dominio As TextBox
    Protected WithEvents destino As TextBox
    Protected WithEvents origen As TextBox
    Protected WithEvents temporal As TextBox
    Protected WithEvents log As TextBox
    'Render this Web Part to the output parameter specified.
    Protected Overrides Sub RenderWebPart(ByVal output As System.Web.UI.HtmlTextWriter)
        log.RenderControl(output)
        output.Write("<br><font>Ruta Origen</font><br>")
        File1.RenderControl(output)
        output.Write("<br><font>Ruta Temporal </font><br>")
        temporal.RenderControl(output)
        output.Write("<br><font>Ruta Destino </font><br>")
        destino.RenderControl(output)
        output.Write("<br><font>Usuario </font><br>")
        usuario.RenderControl(output)
        output.Write("<br><font>Contraseña </font><br>")
        contra.RenderControl(output)
        output.Write("<br><font>Dominio </font><br>")
        dominio.RenderControl(output)
        output.Write("<br><br><center>")
        boton1.RenderControl(output)
        output.Write("</center>")
    End Sub
    Protected Overrides Sub CreateChildControls()
    
        dominio = New TextBox
        With dominio
            .Text = "admon-cfnavarra"
            .Width = Unit.Pixel("255")
        End With
        Controls.Add(dominio)
    
        boton1 = New Button
        With boton1
            .Text = "Copiar Fichero"
        End With
        Controls.Add(boton1)
    
        File1 = New HtmlInputFile
        With File1
    
        End With
        Controls.Add(File1)
    
        usuario = New TextBox
        With usuario
            .Text = "SVCWSINCPre_SNS"
            .Width = Unit.Pixel("255")
        End With
        Controls.Add(usuario)
    
        contra = New TextBox
        With contra
            .Text = "SVCWSINCPre_SNS"
            .Width = Unit.Pixel("255")
        End With
        Controls.Add(contra)
    
        destino = New TextBox
        With destino
            .Text = vdestino
            .Width = Unit.Pixel("255")
        End With
        Controls.Add(destino)
    
        log = New TextBox
        With log
            .Width = Unit.Percentage(100)
            .BackColor = System.Drawing.Color.Black
            .ForeColor = System.Drawing.Color.White
        End With
        Controls.Add(log)
    
        temporal = New TextBox
        With temporal
            .Text = vtemporal
            .Width = Unit.Pixel("255")
        End With
        Controls.Add(temporal)
    End Sub
    Private Sub boton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles boton1.Click
        If File1.PostedFile.FileName <> "" Then
            Dim _objContext As WindowsImpersonationContext = Nothing
            log.Text = QuienSoy()
            CopyFile(File1.PostedFile.FileName, temporal.Text)
            _objContext = Impersonalizacion.WinLogOn(usuario.Text, contra.Text, dominio.Text)
            CopyFile(temporal.Text & "\" & System.IO.Path.GetFileName(File1.PostedFile.FileName), destino.Text)
            _objContext.Undo()
        Else
            log.Text = "Se debe introducir un fichero"
        End If
    End Sub
    Friend Shared Function QuienSoy() As String
        Return WindowsIdentity.GetCurrent().Name
    End Function
    Public Function CopyFile(ByVal StartPath As String, ByVal EndPath As String)
        Try
            Dim fn As String = System.IO.Path.GetFileName(StartPath)
            System.IO.File.Copy(StartPath, EndPath & "\" & fn, False)
            log.Text = "Fichero Copiado Correctamente"
        Catch ex As Exception
            log.Text = ex.Message
        End Try
    End Function
    

    端类