Click here to Skip to main content
15,881,938 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Adding row to datatable programmatically Pin
_AK_10-Feb-09 1:40
_AK_10-Feb-09 1:40 
GeneralRe: Adding row to datatable programmatically Pin
Paul McGann10-Feb-09 2:15
professionalPaul McGann10-Feb-09 2:15 
AnswerRe: Adding row to datatable programmatically Pin
Kaushal Arora10-Feb-09 17:59
Kaushal Arora10-Feb-09 17:59 
QuestionHOW TO OPEN FILE. Pin
anujbanka178410-Feb-09 1:20
anujbanka178410-Feb-09 1:20 
AnswerRe: HOW TO OPEN FILE. Pin
Vasudevan Deepak Kumar10-Feb-09 2:46
Vasudevan Deepak Kumar10-Feb-09 2:46 
AnswerRe: HOW TO OPEN FILE. Pin
Ranjit Viswakumar10-Feb-09 16:39
Ranjit Viswakumar10-Feb-09 16:39 
QuestionNetwork printing Pin
Armandt__10-Feb-09 1:05
Armandt__10-Feb-09 1:05 
AnswerRe: Network printing Pin
Armandt__11-Feb-09 20:05
Armandt__11-Feb-09 20:05 
Ok so no reply from anyone, so I'll leave what I got

When using network resources the asp.net "user" doesn’t have the required rights.
So , what you need to do is impersonate a user on the sever having the rights the access the network .

Create a class, call it whatever you want, I’ll call this one impersonation and add the following:

Some of the imports won’t be needed


   Imports Microsoft.VisualBasic
Imports System.Web.Security
Imports System.Security.Principal
Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.Configuration
Imports SR = System.Reflection
Imports System.Text
Imports System.Web.UI.WebControls

Public Class Impersonation
    Private Const LOGON32_PROVIDER_DEFAULT As Integer = 0
    Private Const LOGON32_LOGON_INTERACTIVE As Integer = 2
    Private Const LOGON32_LOGON_NETWORK As Integer = 3
    Private Const LOGON32_LOGON_BATCH As Integer = 4
    Private Const LOGON32_LOGON_SERVICE As Integer = 5
    Private Const LOGON32_LOGON_UNLOCK As Integer = 7
    Private Const LOGON32_LOGON_NETWORK_CLEARTEXT As Integer = 8
    Private Const LOGON32_LOGON_NEW_CREDENTIALS As Integer = 9

    Private Shared ImpersonationContext As WindowsImpersonationContext

    Declare Function LogonUserA Lib "advapi32.dll" ( _
                            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 Integer

    Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
                            ByVal ExistingTokenHandle As IntPtr, _
                            ByVal ImpersonationLevel As Integer, _
                            ByRef DuplicateTokenHandle As IntPtr) As Integer
    Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
    Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long

    Public Shared Function ImpersonateValidUser(ByVal strUserName As String, _
                   ByVal strDomain As String, ByVal strPassword As String) As Boolean
        Dim token As IntPtr = IntPtr.Zero
        Dim tokenDuplicate As IntPtr = IntPtr.Zero
        Dim tempWindowsIdentity As WindowsIdentity

        ImpersonateValidUser = False

        If RevertToSelf() <> 0 Then
            If LogonUserA(strUserName, strDomain, _
               strPassword, _
               LOGON32_LOGON_INTERACTIVE, _
               LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
                If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
                    tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
                    ImpersonationContext = tempWindowsIdentity.Impersonate()

                    If Not (ImpersonationContext Is Nothing) Then
                        ImpersonateValidUser = True
                        
                    End If
                End If
            End If
        End If

        If Not tokenDuplicate.Equals(IntPtr.Zero) Then
            CloseHandle(tokenDuplicate)
        End If

        If Not token.Equals(IntPtr.Zero) Then
            CloseHandle(token)
        End If

    End Function

    Public Shared Sub UndoImpersonation()

        ImpersonationContext.Undo()

    End Sub
End Class



Now the methods you are going to use are ImpersonateValidUser (with this giving a username and password, domain if required, if no domain only give an empty string as parameter) and UndoImpersonation. The username and password is a user that’s on the server your application is running.

How to use it, when you need to run a piece of code that needs impersonation, call ImpersonateValidUser, and under it what you want to do and then again UndoImpersonation. I have found that when not undoing the impersonation reports will not load on your website, an error saying report in use, only reason for this I can think of is that the asp user is allready using the reports and when you impersonate that user is also trying to use the report.
Now the thing I was trying to accomplish was to print to a client pc automatically. The only way for this to happen is when the server can get access to the clients pc, thus sharing it.
The only problem I have is on one of the printers I was trying to use I keep getting a error, RPC server not running, so if anyone can give me a hand on this.

Thanks
Questionopening excel document Pin
kulandaivel_mca200710-Feb-09 1:04
kulandaivel_mca200710-Feb-09 1:04 
AnswerRe: opening excel document Pin
support synthesys10-Feb-09 2:06
support synthesys10-Feb-09 2:06 
GeneralRe: opening excel document [modified] Pin
kulandaivel_mca200710-Feb-09 2:14
kulandaivel_mca200710-Feb-09 2:14 
Questionthumbnail image in datalist and display full size image on any of datalist image click Pin
Member 398136610-Feb-09 0:49
Member 398136610-Feb-09 0:49 
QuestionIFrame Pin
trinm198710-Feb-09 0:36
trinm198710-Feb-09 0:36 
AnswerRe: IFrame Pin
Vasudevan Deepak Kumar10-Feb-09 2:48
Vasudevan Deepak Kumar10-Feb-09 2:48 
QuestionUnable to pass values through soapcontext Pin
Sundar_R10-Feb-09 0:30
Sundar_R10-Feb-09 0:30 
QuestionHow to perform grid view operations like select,update delete from database in asp.net 2.0? Pin
Member 46630879-Feb-09 23:34
Member 46630879-Feb-09 23:34 
AnswerRe: How to perform grid view operations like select,update delete from database in asp.net 2.0? Pin
Paddy Boyd10-Feb-09 0:23
Paddy Boyd10-Feb-09 0:23 
Questionsmalldatetime in repeater control Pin
radupopescu7779-Feb-09 23:21
radupopescu7779-Feb-09 23:21 
AnswerRe: smalldatetime in repeater control Pin
Bardy8510-Feb-09 1:02
Bardy8510-Feb-09 1:02 
GeneralRe: smalldatetime in repeater control Pin
radupopescu77710-Feb-09 1:30
radupopescu77710-Feb-09 1:30 
Questionload the mail box contact in dropdown Pin
Glossy9-Feb-09 23:11
Glossy9-Feb-09 23:11 
AnswerRe: load the mail box contact in dropdown Pin
Ranjit Viswakumar10-Feb-09 16:48
Ranjit Viswakumar10-Feb-09 16:48 
GeneralRe: load the mail box contact in dropdown Pin
Glossy10-Feb-09 19:43
Glossy10-Feb-09 19:43 
QuestionEvents in ASP.NET Page Pin
Kit Fisto9-Feb-09 23:04
Kit Fisto9-Feb-09 23:04 
AnswerRe: Events in ASP.NET Page Pin
Paddy Boyd10-Feb-09 0:24
Paddy Boyd10-Feb-09 0:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.