A better way to get a Windows Username using Forms Authentication?

前端 未结 2 1692
名媛妹妹
名媛妹妹 2021-01-07 00:09

I\'ve inherited a project that uses forms authentication, but a recent feature request requires me to get the Windows username of some users.

It\'s a website that us

2条回答
  •  盖世英雄少女心
    2021-01-07 00:58

    You could use directory services

    PrincipalContext pc = null;
    UserPrincipal principal = null;
    var username = User.Identity.Name;
    pc = new PrincipalContext(ContextType.Domain, "give your domain name here");
    principal = UserPrincipal.FindByIdentity(pc, userName);
    var firstName = principal.GivenName ?? string.Empty;
    var lastName = principal.Surname ?? string.Empty;
    

    please add reference for System.DirectoryServices.AccountManagement

提交回复
热议问题