Saturday, July 14, 2007

How Forms Authentication is differ from Windows Authentication in adding users to the Group in object model

When you have forms authentication enabled for your site and adding users to Group is not as simple as you do with windows authentication in object model. In windows authentication to add the users to the group(assumption: the current domain user has the permission to add the user to group) we have In-built webservice http://server/site/_vti_bin/UserGroup.asmx and using AddUserToGroup method from it.
note: here the site should be any windows authentication site.
. But in forms authentication its not going to be work. The reason is that the forms tries to access the http://server/site/_vti_bin/UserGroup.asmx service it will redirect to the login page of the current working site. Bcoz the user is not windows impersonated so he doesn't has the permission to access. The work around is to impersonate webservice with user windows domain credentials. If the webreference name is "GroupUser" follow below steps:

  • GroupUser obj=new GroupUser();
  • System.Net.NetworkCredential cred = new NetworkCredential(UserName,Password,Domain); [Import system.Net namespace]
  • obj.Credentials=System.Net.CredentialCache.DefaultNetworkCredentials;
  • Now call the method as obj.AddUserToGroup(groupName As String,userName As String,userLoginName As String,userEmail As String,userNotes As String)

This will work out.

No comments: