在 citrix session 中运行时,C# 用于确定客户端 ip 地址?

标签 c# ip-address citrix

我目前正在使用 WFAPI 从 C# 中确定 citrix session 的客户端 ip 地址

[StructLayout(LayoutKind.Sequential)]
internal struct WF_CLIENT_ADDRESS {
    public int AddressFamily;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public byte[] Address;
}

[DllImport("WFAPI.dll", EntryPoint = "WFFreeMemory")]
private static extern void WFFreeMemory(IntPtr pMemory);

[DllImport("WFAPI.dll", EntryPoint = "WFQuerySessionInformationA")]
private static extern bool WFQuerySessionInformation(IntPtr hServer, 
    int iSessionId, int infotype, out IntPtr ppBuffer, out int pBytesReturned);

const int ClientAddress = 14;
const int CurrentSession = -1;
static readonly IntPtr CurrentServer = IntPtr.Zero;

public static string GetClientAddress() {
    IntPtr addr;
    int returned;
    bool ok = WFQuerySessionInformation(CurrentServer, CurrentSession, 
        ClientAddress, out addr, out returned);
    if (!ok) return null;
    WF_CLIENT_ADDRESS obj = new WF_CLIENT_ADDRESS();
    obj = (WF_CLIENT_ADDRESS)Marshal.PtrToStructure(addr, obj.GetType());
    string clientAdress = 
        obj.Address[2] + "." + obj.Address[3] + "." + 
        obj.Address[4] + "." + obj.Address[5];
    WFFreeMemory(addr);
    return clientAdress;
}

WFAPI.DLL/WFAPI64.DLL 似乎在我有权访问的 citrix 环境中可用。 有没有人有更好的方法来做到这一点?
有谁知道如何确定该进程是否实际上在 citrix 环境中运行?

最佳答案

不,你正在做的很好。 WFAPI 是获取此类信息的最佳方式之一。

了解 session 是否为 Citrix session 只是您所做工作的延伸。如果您查看 WFQuerySessionInformation 的 WFAPI 引用:

http://community.citrix.com/download/attachments/37388956/WFAPI_SDK_Documentation.pdf

更新链接:WinFrame API SDK

查看 WFInfoClass 值表。您会看到许多参数带有标记“3”,表示它们仅在 ICA session 内部调用时可用。因此,您可以使用其中之一调用 WFQuerySessionInformation,如果它返回 false,则您没有在 Citrix session 中运行。您当前正在执行的 IP 地址查询是这些属性之一,因此当您的“ok”变量为 false 时,您不在 Citrix session 中。

还有一些有趣的事情,Microsoft 提供了与 WFAPI 非常相似的 WTS api,并且做的事情基本相同。但是,WFAPI 的优势在于它可以与 XenDesktop 和 XenApp 一起使用,而 WTS API 仅可以与 XenApp 一起使用。

问候, 多诺万

关于在 citrix session 中运行时,C# 用于确定客户端 ip 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9603289/

相关文章:

c# - 在 C# 代码中运行 PSCmdLets (Citrix XenDesktop)

asp.net - 该页面的状态信息无效,并且可能已损坏,现在无法工作(在 Citrix 下)

c# - C# 中的新运算符不会覆盖基类成员

ip - 我可以捕获登录我网站的每个用户的 IP 地址吗

c# - 嵌套自身的类的 Protobuf-net 序列化

laravel - Chrome 使用了错误的本地 IP 地址

ios - 如何在 iOS 应用程序中获取手机 IP 地址

javascript - 我需要做任何事情使我的Electron应用程序 “Citrix compatible”吗?

c# - 将匿名类型转换为 List<KeyValuePair>

c# - 我如何模拟 HttpRequest[] 索引器属性