Hi Lakshimi,
Plz gothrough this code i think it is usefull .
Here is what the web.config looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<authentication mode="Windows">
<forms name="imper2" protection="All" timeout="80" loginUrl="login.aspx" path="/"></forms>
</authentication>
<identity impersonate="true"></identity>
<authorization>
<deny users="?"></deny>
</authorization>
</system.web>
</configuration>
Here is what the login.aspx looks like
| | <%@ Language=VBScript Debug="True" %> |
| | <%@ Import Namespace="System.Web" %> |
| | <%@ Import Namespace="System.Security.Principal" %> |
| | <%@ Import Namespace="System.Runtime.InteropServices" %> |
| | <%@ Import Namespace="System" %> |
<html>
<head>
<script runat="server">
Dim LOGON32_LOGON_INTERACTIVE As Integer = 3
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
Declare Auto Function LogonUser Lib "advapi32.dll" (lpszUsername As String, _
lpszDomain As String, _
lpszPassword As String, _
dwLogonType As Integer, _
dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Integer
Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr, _
ImpersonationLevel As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Integer
Instantiate a WindowsImpersonationContext
Dim impersonationContext As WindowsImpersonationContext
Dim currentWindowsIdentity As WindowsIdentity
The page load function this page loads on startup
Sub Button_Click(s As Object, e As EventArgs)
If impersonateValidUser(txtUsername.Text, txtDomain.Text, txtPassword.Text) Then
Response.Write(User.Identity.Name)
undoImpersonation()
Else
Response.Write("We're sorry, we were not able to impersonate you!")
End If
End Sub
Private function impersonateValidUser() returns a boolean indicating
if the function call was successful or failed
Private Function impersonateValidUser(userName As String, domain As String, password As String) As Boolean
'Dim userName As String = txtUsername.Text
'Dim domain As String = txtDomain.Text
'Dim password As String = txtPassword.Text
Dim tempWindowsIdentity As WindowsIdentity
Dim token As IntPtr
Dim tokenDuplicate As IntPtr
If LogonUser("teacher", "mydomain", "*****", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
If DuplicateToken (token, 3, tokenDuplicate) <> 0 Then
tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
impersonationContext = tempWindowsIdentity.Impersonate()
If impersonationContext is Nothing Then
impersonateValidUser = false
Else
impersonateValidUser = true
End If
Else
impersonateValidUser = false
End If
Else
impersonateValidUser = false
End If
End Function
'reverse the impersonation operation and go back to the original permissions
Private Sub undoImpersonation()
impersonationContext.Undo()
End Sub
</script>
<meta name="GENERATOR" Content="Microsoft Visual Studio.NET 7.0">
</head>
<body>
<form runat="server">
<h3>Please login here:</h3>
<hr>
Username:
<asp:TextBox Runat="server" ID="txtUsername" TextMode=SingleLine></asp:TextBox>
<br>
Domain:
<asp:TextBox Runat="server" ID="txtDomain" TextMode=SingleLine></asp:TextBox>
<br>
Password:
<asp:TextBox Runat="server" ID="txtPassword" TextMode=Password></asp:TextBox>
<br>
<asp:Button Runat="server" Text="Login" OnClick="Button_Click"></asp:Button>
</form>
</body>
</html>
ThanksNarasima |