Asp.net network access path (simulate user logon), asp.net user logon _ PHP Tutorial

Source: Internet
Author: User
Asp.net method for accessing the network path (simulating user logon) and asp.net user logon. Asp.net access network path method (simulated user logon), asp.net user logon core code: publicclassIdentityScope: IDisposable {obtainsusertoken [DllImport (advapi32.dll, asp.net access network path method (simulated user logon ), asp.net user logon

Core code:

Public class IdentityScope: IDisposable {// obtains user token [DllImport ("failed", 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 bool CloseHandle (IntPtr handle ); [DllImport ("Advapi32.DLL")] static extern bool token (IntPtr hToken); [DllImport ("Advapi32.DLL")] static extern bool RevertToSelf (); const int token = 0; const int LOGON32_LOGON_NEWCREDENTIALS = 9; // What is the domain risk control? In D? Do you want a to use? : Interactive = 2 private bool disposed ;////// Login? ? //////Use? User § Name?///Domain region name ?,? Such? Result? No? In the ú region, where is D used for 1? Host IP address? Address ·///Password?Public IdentityScope (string sUsername, string sDomain, string sPassword) {// initialize tokens IntPtr pExistingTokenHandle = new IntPtr (0); IntPtr pDuplicateTokenHandle = new IntPtr (0 ); try {// get handle to token bool bImpersonated = LogonUser (sUsername, sDomain, sPassword, login, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle); if (true = bImpersonated) {if (! ImpersonateLoggedOnUser (pExistingTokenHandle) {int nErrorCode = Marshal. getLastWin32Error (); throw new Exception ("ImpersonateLoggedOnUser error; Code =" + nErrorCode) ;}} else {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 void Dispose (bool disposing) {if (! Disposed) {RevertToSelf (); disposed = true ;}} public void Dispose () {Dispose (true );}}

The second parameter is the domain name. if there is a domain name, write the domain name. if there is no domain name, write the IP address of the target machine.

using (IdentityScope c = new IdentityScope("administrator", "192.168.0.1", "11111")){ string[] filelist = System.IO.Directory.GetDirectories(@"\\192.168.0.1\folderName");}


Aspnet simulated user login problems

Configure it in web. config.


Aspnet-related: the user has logged on. how can I prevent the user from accessing a page that he has no permission to access by copying a webpage path?

You can try to create a table, save the page name, save the user, and then another relational table, page number and user number.

Login (simulated user login), asp.net core user login code: public class IdentityScope: IDisposable {// obtains user token [DllImport ("advapi32.dll ",...

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.