Asp.net uses the specified username and password to simulate login to access the network path File

Source: Internet
Author: User

When work is used, record it, that is, there is a shared directory on the network path, assign you an account and password with certain permissions to read and write, do not make changes in the configuration file, relatively safe. The windowsidentity. Impersonate method is used. Hope to give it to your friends.

 

Main class identityscope

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. runtime. interopservices;

///   <Summary>
/// Abstract description of identityscope
///   </Summary>
Public Class Identityscope: idisposable
{
// Obtains User Token
[Dllimport ( " Advapi32.dll " , Setlasterror = True )]
Static Extern Bool Logonuser ( String Pszusername, String Pszdomain, String Pszpassword, Int Dwlogontype, Int Dwlogonprovider, Ref Intptr phtoken );

//Closes open handes returned by logonuser

[Dllimport ("Kernel32.dll", Charset = charset. Auto)]

Extern Static BoolClosehandle (intptr handle );

[Dllimport ("Advapi32.dll")]
Static Extern BoolImpersonateloggedonuser (intptr htoken );

[Dllimport ("Advapi32.dll")]
Static Extern BoolReverttoself ();

// Logon types
Const Int Logon32_logon_interactive = 2 ;
Const Int Logon32_logon_network = 3 ;
Const Int Logon32_logon_new_credentials = 9 ;
// Logon providers
Const Int Logon32_provider_default = 0 ;
Const Int Logon32_provider_winnt50 =3 ;
Const Int Logon32_provider_winnt40 = 2 ;
Const Int Logon32_provider_winnt35 = 1 ;

Private BoolDisposed;

///   <Summary>
/// Login
///   </Summary>
///   <Param name = "susername"> User Name </Param>
///   <Param name = "sdomain"> The second parameter is the domain name. If there is a domain name, the domain name is written, and no domain name is written into the IP address of the target machine · </Param>
///   <Param name = "spassword"> Password </Param>
Public Identityscope ( String Susername, String Sdomain, String Spassword)
{

//Initialize tokens
Intptr pexistingtokenhandle =NewIntptr (0);
Intptr pduplicatetokenhandle =NewIntptr (0);
Try
{

// Get handle to token
Bool Bimpersonated = logonuser (susername, sdomain, spassword, logon32_logon_interactive, logon32_provider_default, Ref Pexistingtokenhandle );
If ( True = Bimpersonated)
{
If (! Impersonateloggedonuser (pexistingtokenhandle ))
{
Int Nerrorcode = marshal. getlastwin32error ();
Throw New Exception ( " Impersonateloggedonuser error; code = " + Nerrorcode );
}

}< br> else
{< br> int nerrorcode = marshal. getlastwin32error ();
throw New exception (" logonuser error; code = " + nerrorcode);

}

}
Finally
{
//Close handle (s)
If(Pexistingtokenhandle! = Intptr. Zero)

Closehandle (pexistingtokenhandle );

If(Pduplicatetokenhandle! = Intptr. Zero)

Closehandle (pduplicatetokenhandle );
}

}

Protected Virtual VoidDispose (BoolDisposing)
{

If(! Disposed)
{
Reverttoself ();
Disposed =True;
}

}

Public VoidDispose ()
{
Dispose (True);
}


}

 

Call

 

Using (Identityscope c = New Identityscope ( " User Name " , " Domain/IP " , " Password " ))
{
String [] Filelist = system. Io. Directory. getdirectories ( @" \ 192.168.1.7 \ test " );
}

 

Note:

 

1. If the computer you access is located in the domain, enter the domain name and modify

Bool

Bimpersonated = logonuser (susername, sdomain, spassword, logon32_logon_interactive, logon32_provider_default,RefPexistingtokenhandle );

 

2. If the computer you are accessing is not in the domain, you can pass in the IP address and modify

 

Bool Bimpersonated = logonuser (susername, sdomain, spassword, logon32_logon_new_credentials, logon32_provider_default, Ref Pexistingtokenhandle );

 

3. If the "Logon Failed: Unknown user name or wrong password" error is reported, it indicates that your parameter is incorrect. Pay attention to the fourth parameter of logonuser.

4. Everyone may encounter different situations, but I hope to help you.

 

 

Msdn:

Http://msdn.microsoft.com/zh-cn/library/w070t6ka.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.