1

I'm working on a project where the client wants to restrict some content to only Active Directory users . 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). Something like SPUser.IsADUser would be awesome.

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)

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
}
Jordan C.
  • 54
  • 7
  • That seems to me the better way to do it. I will look in to this thread to see if someone comes up with a better solution. – Luis Oct 27 '13 at 13:04

1 Answers1

0

In my experience it is much better to use audiences for this purpose. You then can easily trim any web part using "Audience" property. You can read about audiences here. Of course it will only work if you have user profile synchronization configured.

Yevgeniy.Chernobrivets
  • 3,194
  • 2
  • 12
  • 14
  • Thanks, but unfortunately this won't for this the client. – Jordan C. Oct 31 '13 at 17:29
  • If it is because you do not have User profile services running then you can use sharepoint group. It will work the same as if it is audience. The only difference that you need to populate this group manually or add custom code to do this. – Yevgeniy.Chernobrivets Nov 04 '13 at 07:36
  • One more thing i wanted to ask - what do you mean by saying "AD User"? If you have NTLM authentication then all your users are domain users. Could you please provide an example of non AD user? – Yevgeniy.Chernobrivets Nov 04 '13 at 07:37