从服务主机获取用户名

时间:2013-09-17 11:54:49

标签: c# wcf web-services web-config

我创建了一个由IIS托管的WCF服务。它提供了一个合同(接口)中指定的一些方法。这很好用,没有问题。

    <services>
        <service name="myNamespace.KliAPI" behaviorConfiguration="ServiceBehavior">
            <endpoint address="" behaviorConfiguration="largeDataBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" name="test" contract="myNamespace.ImyAppAPI" />
            <endpoint address="help" kind="webHttpEndpoint" behaviorConfiguration="helpPageBehavior" contract="myNamespace.ImyAppAPI" />
        </service>
    </services>

我使用customAuthenticator是web.config来处理登录逻辑。它根据数据库验证用户名和密码,并返回TRUE或FALSE,具体取决于凭据是否与数据库记录匹配。

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="CustomAuthenticator" type="myNamespace.CustomAuthenticator, myNamespace.IISServiceHost" />
    </modules>
</system.webServer>

问题: 我希望能够获取/读取通过合同接口 ImyAppAPI 公开的方法中提供给CustomAuthenticator类的用户名。如何将该信息从主机共享到服务方法?我认为它特别棘手,因为暴露的方法不是以编程方式进行的,而是通过在web.config文件中指定合同自动生成的。

示例:

public string[] DownloadUserSpecificData(int[] filters){
    //Here I want access to the username
}

1 个答案:

答案 0 :(得分:1)

怎么样?
ServiceSecurityContext.Current.PrimaryIdentity.Name