从 WCF 服务器端获取 Windows 用户名 [英] Get Windows Username from WCF server side

查看:25
本文介绍了从 WCF 服务器端获取 Windows 用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Web 服务和 WCF 非常熟悉,而且我使用的是 Windows 集成身份验证 - 如何在服务器端界面上获取用户名?我相信我应该实现自定义行为,或者可能是 WCF Sessions 的东西?任何线索都会非常方便.

I'm pretty green with web services and WCF, and I'm using Windows integrated authentication - how do I get the username on the server-side interface? I believe that I'm supposed to implement a custom Behavior, or perhaps something with WCF Sessions? Any clues would be super-handy.

推荐答案

以下是一段服务代码,展示了如何检索和使用与 WCF 服务调用方关联的 WindowsIdentity.

Here is a snippet of service code that shows how you could retrieve and use the WindowsIdentity associated with the caller of a WCF service.

此代码假设您接受大多数默认配置.使用命名管道或 Net TCP 绑定时,它应该可以正常工作.

This code is assuming that you are accepting most of the defaults with your configuration. It should work without any problems with the Named Pipe or the Net TCP binding.

p.Demand() 将确定用户是否在由 permissionGroup 变量指定的 windows 组中.

the p.Demand() will determine if the user is in the windows group specified by the permissionGroup variable.

private static void DemandManagerPermission()
{
    // Verify the use has authority to proceed
    string permissionGroup = ConfigurationManager.AppSettings["ManagerPermissionGroup"];
    if (string.IsNullOrEmpty(permissionGroup))
        throw new FaultException("Group permissions not set for access control.");

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    var p = new PrincipalPermission(ServiceSecurityContext.Current.WindowsIdentity.Name, permissionGroup, true);
    p.Demand();

}

这篇关于从 WCF 服务器端获取 Windows 用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆