2008-10-24 100 views
3

我需要访问只有给定的域帐户才能访问的网络资源。 我正在使用LogonUser调用,但得到“用户没有必需的特权”异常,因为Web应用程序正在使用asp.net帐户运行,并且它没有足够的权限来进行此调用。需要模拟用户访问网络资源,Asp.Net帐户

有没有办法避开它? 更改ASP.Net帐户的身份或权限不是一种选择,因为这是一个运行许多项目的生产计算机。 有没有更好的方法来实现这一目标?

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

亲切的问候。

回答

7

只要调用LogonUser是不够的。您需要模拟该用户。您只能模拟对网络资源的访问。

示例代码可在MSDN上找到。

1

你可以一个

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

标签添加到您的web.config但你可能不希望运行的整个网站为用户可能不会很理想......

灿您将网络共享映射为本地驱动器与DomainName &密码...然后通过映射的驱动器将文件拖到网站上?

NET USE Z: \\SERVER\Share password /USER:DOMAIN\Username /PERSISTENT:YES 
0

我只有这种亲密体验下1.1,这样的事情可能会在2.0天甲肝改变,但是...... 我们已经得到了被部署在Intranet方案的应用,我们罢工一样。我们运行身份模拟打开,窗体模式身份验证,匿名访问关闭。控制这个(我发现)的最简单方法是将有权访问的用户的凭据放在web.config中。他们继续转换身份模拟的节点。如果是超级恐怖信息,我不会这样做!我们只在打印环境中访问共享图形,因此大多数网站都很乐意为我们设置一个有限帐户,以便将其放入web.confit。 LogonUser确实需要提升privelidges。 Msdn有一些关于如何在代码中模拟特定用户的好文章。我会找出一些链接,但这款手机不会复制粘贴。

0

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

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

0
  • 有了这个的WebPartŸ连接到限制访问的网络资源,我把文件和y关闭与资源(与授权访问的用户)的连接,你不需要做一个新的共享连接,这是只有限制,我的系统离开对我做出。也许,有许多进口是必要的,但我做了很多测试,我没有时间清理代码。我希望对你有所帮助。 (抱歉我的英文不好)。

进口系统 进口System.ComponentModel 进口System.Web.UI程序 进口System.Web.UI.WebControls 进口System.IO 进口有System.IO.File 进口System.Diagnostics程序 导入系统。 Xml.Serialization 进口Microsoft.SharePoint程序 进口Microsoft.SharePoint.Utilities 进口Microsoft.SharePoint.WebPartPages 进口Microsoft.SharePoint.WebControls 进口Microsoft.SharePoint.Administration 进口SYSTE m.Security.Principal 进口System.Security.Permissions 进口System.Runtime.InteropServices 进口统环境 进口的System.Net.Sockets 进口System.Web.UI.HtmlControls

公共类Impersonalizacion 私人常量LOGON32_PROVIDER_DEFAULT为整数= 0 私人CONST 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(名字空间:=” 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 

末级