简体   繁体   English

如何判断SPUser是否为Active Directory帐户

[英]How to tell if SPUser is Active Directory account

I'm working on a project where the client wants to restrict some content to only Active Directory users . 我正在一个项目中,客户端希望将某些内容限制为仅Active Directory用户。 Is there any way to identify that a SPUser is an AD user short of parsing the username string for the domain (or something along those lines). 没有任何方法可以识别SPUser是AD用户,而不需要解析域的用户名字符串(或类似的内容)。 Something like SPUser.IsADUser would be awesome. 像SPUser.IsADUser这样的东西真棒。

Edit This seems to work, but I'm not sure if this is reliable enough? 编辑这似乎可行,但是我不确定这是否足够可靠? For this use case, identifying that a user is a windows user is enough (there are no local system accounts) 对于此用例,只需确定用户是Windows用户就足够了(没有本地系统帐户)

SPUser user = SPContext.Current.Web.CurrentUser;
string userName = user.LoginName.Substring(user.LoginName.IndexOf('|') + 1);
SPPrincipalInfo info = SPUtility.ResolveWindowsPrincipal(SPContext.Current.Site.WebApplication, userName, SPPrincipalType.User, false);
if(info != null){
    //THIS IS A WINDOWS ACCOUNT
}

In my experience it is much better to use audiences for this purpose. 以我的经验,为此目的最好使用受众。 You then can easily trim any web part using "Audience" property. 然后,您可以使用“受众群体”属性轻松修剪任何Web部件。 You can read about audiences here . 您可以在此处阅读有关受众的信息 Of course it will only work if you have user profile synchronization configured. 当然,只有在配置了用户配置文件同步的情况下,它才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM