Get Windows Username from WCF server side

后端 未结 4 1261
孤街浪徒
孤街浪徒 2020-12-09 09:52

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 s

相关标签:
4条回答
  • 2020-12-09 10:07

    Try looking at ServiceSecurityContext.Current.WindowsIdentity

    0 讨论(0)
  • 2020-12-09 10:11

    To get the WCF Service caller username:

    var callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;

    0 讨论(0)
  • 2020-12-09 10:21

    have you tried WindowsIdentity.GetCurrent();?

    0 讨论(0)
  • 2020-12-09 10:24

    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.

    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.

    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();
    
    }
    
    0 讨论(0)
提交回复
热议问题