繁体   English   中英

需要模拟用户以访问网络资源,Asp.Net帐户

[英]Need to Impersonate user forAccessing Network resource, Asp.Net Account

我需要访问仅给定域帐户可以访问的网络资源。 我正在使用LogonUser调用,但由于Web应用程序正在使用asp.net帐户运行,并且没有足够的权限进行此调用,因此出现“用户没有所需的特权”异常。

有办法解决吗? 不能更改ASP.Net帐户的标识或权限,因为这是一台运行许多项目的生产机器。 有没有更好的方法来实现这一目标?

使用Asp.Net 2.0,进行表单身份验证。

亲切的问候。

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

可以在MSDN上找到示例代码。

您可以添加一个

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

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

您可以使用DomainName&Password ...将网络共享映射为本地驱动器,然后通过映射的驱动器将文件拉到网站吗?

NET USE Z: \\SERVER\Share password /USER:DOMAIN\Username /PERSISTENT:YES
  • 将此WebPart y连接到具有受限访问权限的网络资源后,我放了一个文件并y关闭了与该资源的连接(作为具有授予访问权限的用户),您无需建立新的共享连接,这仅是限制,部门对我有利。 可能是,有许多必要的导入,但是我做了很多测试,没有时间清理代码。 希望对您有所帮助。 (对不起,我的英语不好)。

导入系统导入System.ComponentModel导入System.Web.UI导入System.Web.UI.WebControls导入System.IO导入System.IO.File导入System.Diagnostics导入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.Environment导入System.Net.Sockets导入System.Web .UI.HtmlControls

公共类非个性化私有常量LOGON32_PROVIDER_DEFAULT作为整数= 0私有常量LOGON32_LOGON_INTERACTIVE作为整数= 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(Namespace:=” SPSCopiarFichero“)> _公共类WebPart1继承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

末级

我在1.1之下只有很丰富的经验,所以2.0天内可能会发生变化,但是...我们有一个部署在Intranet场景中的应用程序,并且遇到了同样的事情。 我们在启用身份模拟,表单模式身份验证,匿名访问已关闭的情况下运行。 控制此问题(我发现)的最简单方法是将具有访问权限的用户的凭据放入web.config中。 它们在您启用身份模拟的节点上进行。 如果它是超级秘密信息,我还是不会这样! 我们仅在打印环境中访问共享图形,因此大多数站点很乐意为我们设置一个受限帐户以放入web.confit。 LogonUser确实确实需要提升的特权。 Msdn上有一些不错的文章,介绍了如何在代码中模拟特定用户。 我想找出一些链接,但是这部手机没有复制粘贴。

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

这使我可以将Windows服务作为本地系统运行,并可以访问受保护的网络资源。 而且,这种技巧似乎也适用于作为网络服务运行的ASP.NET进程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM