C # Try to read or write the protected memory. This usually indicates that other memory is damaged.

Source: Internet
Author: User

Generally, there are several ways to verify IIS. The most common ones are Anonymous Authentication and Windows Authentication integration. However, integrating windows Verification involves a dialog box every time, which is very troublesome, especially when proxy is used.

So how can I implement windows verification using a form? Of course, just think about it. I am not familiar with web development, and Windows is even worse.

One method is found on the Internet. Using the logonuser method of advapi32.dll, you can verify that the user name and password you entered are valid users of windows.

CodeAs follows:

'Include permissions namespace for security attributes
'Include principal namespace for windowsidentity class
'Include interopservices namespace for dllimports.

Imports system. Security. Principal
Imports system. Security. Permissions
Imports system. runtime. interopservices

'<Assembly: securitypermissionattribute (securityaction. requestminimum, unmanagedcode: = true)>
Public class loginuser

<Dllimport ("C: // windows // system32 // advapi32.dll")> _
Private 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 integer) as Boolean
End Function

<Dllimport ("C: // windows // system32 // kernel32.dll")> _
Private shared function getlasterror () as integer
End Function

Public shared function loginthisuser (byval username as string, byval domainname as string ,_
Byval password as string) as string

Try
'The Windows NT User Token.
Dim token1 as integer

'Get the User Token for the specified user, machine, and password using the unmanaged logonuser method.

'The parameters for logonuser are the user name, computer name, password,
'Logon type (logon32_logon_network_cleartext), logon provider (logon32_provider_default ),
And User Token.
Dim loggedon as Boolean = logonuser (username, domainname, password, 3, 0, token1)

'Impersonate user
Dim token2 as intptr = new intptr (token1)
Dim mwic as windowsimpersonationcontext = _
New windowsidentity (token2). Impersonate

Catch e as exception
'Error occurred... error number unknown.
Return "Err. occurred:" & E. Message
End try

Dim winuser as windowsidentity
Return winuser. getcurrent. Name
End Function

End Class

 

However, I am used to C # for VB code on the Internet, so I want to convert a line by line. As a result, an error occurred while executing the command: The system prompts you to try to read or write the protected memory. This usually indicates that other memory is damaged.

In the beginning, it was thought that the System user information was to be obtained, so the required permissions were relatively high or the protected system information was read. Continue to search for this error and find that it is because of the use of unmanaged code, and when I convert the last variable byref phtoken as integer IN THE logonuser function to an int, it should actually be the ref inptr type, this error is reported because the parameter of the function that references the unmanaged code is incorrect.

The differences between managed code and non-managed code are further studied ..

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.