Connecting to Wi-Fi in C# - EAP TTLS user data & WlanSetProfileEapXmlUserData

孤街醉人 提交于 2020-06-16 21:36:28

问题


I'm attempting to connect to a EAP-TTLS Wi-Fi profile in C#.

I have the below native method:

[DllImport("Wlanapi.dll", SetLastError = true)]
public static extern uint WlanSetProfileEapXmlUserData(
    IntPtr hClientHandle,
    ref Guid pInterfaceGuid,
    string strProfileName,
    uint dwFlags,
    string strEapXmlUserData,
    IntPtr pReserved);

I call it as below:

var result = NativeMethods.WlanSetProfileEapXmlUserData(clientHandle, ref interfaceGuid, "MyWifiProfileName", WLAN_SET_EAPHOST_DATA_ALL_USERS, profileData, IntPtr.Zero);

profileData is as below:

<?xml version="1.0" encoding="UTF-8" ?>
<EapHostUserCredentials xmlns="http://www.microsoft.com/provisioning/EapHostUserCredentials"
xmlns:eapCommon="http://www.microsoft.com/provisioning/EapCommon"
xmlns:baseEap="http://www.microsoft.com/provisioning/BaseEapMethodUserCredentials">
<EapMethod>
<eapCommon:Type>21</eapCommon:Type>
<eapCommon:AuthorId>311</eapCommon:AuthorId>
</EapMethod>
<Credentials xmlns="http://www.microsoft.com/provisioning/EapHostUserCredentials">
<EapTtls xmlns="http://www.microsoft.com/provisioning/EapTtlsUserPropertiesV1">
<Username>%%%USERNAME%%%</Username>
<Password>%%%PASSWORD%%%</Password>
</EapTtls>
</Credentials>
</EapHostUserCredentials>

Yet I can never seem to get a successful response. 'result' is always 1168 - ERROR_NOT_FOUND - Element not found. LastWin32Error is always 1008 - ERROR_NO_TOKEN.

Even when I change the profile name to a profile that doesn't exist, or the XML to something clearly invalid, I still get the same exit code and last error.

What am I doing wrong?


回答1:


Received an answer from the MSDN forum (link)

Resolution was a different declaration for the native method:

    [DllImport("wlanapi.dll")]
    public static extern int WlanSetProfileEapXmlUserData(
        [In] IntPtr clientHandle,                                   // The client's session handle, obtained by a previous call to the WlanOpenHandle function.
        [In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid, // The GUID of the interface.
        [In, MarshalAs(UnmanagedType.LPWStr)] string profileName,   // The name of the profile associated with the EAP user data. Profile names are case-sensitive. This string must be NULL-terminated.
        [In] SetEapUserDataMode dwFlags,                            // A set of flags that modify the behavior of the function.
        [In, MarshalAs(UnmanagedType.LPWStr)] string userDataXML,   // A pointer to XML data used to set the user credentials, The XML data must be based on the EAPHost User Credentials schema. To view sample user credential XML data, see EAPHost User Properties: http://msdn.microsoft.com/en-us/library/windows/desktop/bb204765(v=vs.85).aspx
        IntPtr reservedPtr
    );


来源:https://stackoverflow.com/questions/62307389/connecting-to-wi-fi-in-c-sharp-eap-ttls-user-data-wlansetprofileeapxmluserda

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!