none
401 un authorized error RRS feed

  • Question

  • Hi all,

    Iam using below code to add user to group.The site is configured ldap with fba integration in sharepoint 2010.Iam getting 401 un authorized error in ctx.executequery line even i have access to site.Please help.Below is my complete code.

                                              

    string siteUrl = "http://gauti.sharepoint.com/Testsubsite";  

                ClientContext clientContext = new ClientContext(siteUrl); 
                 clientContext.Authenticationmode=Default;
                clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
                ctx.ExecutingWebRequest +=
    new EventHandler<WebRequestEventArgs>(ctx_MixedAuthRequest);
                SP.List oList = clientContext.Web.Lists.GetByTitle("ScMasterList");  

                CamlQuery camlQuery = new CamlQuery();  
                camlQuery.ViewXml = "<View></View>";  
                ListItemCollection collListItem = oList.GetItems(camlQuery);  

                clientContext.Load(collListItem);  

                clientContext.ExecuteQuery();  

                foreach (ListItem oListItem in collListItem)  
                {  
                    scurl=oListItem["url"].ToString(); 
                    ClientContext ctx= new ClientContext(scurl); 
                    ctx.Authenticationmode=Default;
                ctx.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
                ctx.ExecutingWebRequest +=
    new EventHandler<WebRequestEventArgs>(ctx_MixedAuthRequest);
                    GroupCollection siteGroups = ctx.Web.SiteGroups; 
                    Group membersGroup = siteGroups.GetById(5); 

                    Web web=ctx.web()
                    ctx.Load(web)
                   string prefix="i:0#w.t|sasass|"
                   string username=prefix+txtLogname.Text
                   User usr = web.EnsureUser(userName);
                   ctx.Load(usr);
                   ctx.ExecuteQuery();
                    logname = usr.LoginName;"

                    if(logname!="")
                     {
                    UserCreationInformation userCreationInfo = new UserCreationInformation(); 
                    userCreationInfo.LoginName = logname; 
                    User newUser = membersGroup.Users.Add(userCreationInfo); 
                    ctx.ExecuteQuery(); 
                 }

     void ctx_MixedAuthRequest(object sender, WebRequestEventArgs e)
    {
    try
    {
    //Add the header that tells SharePoint to use Windows authentication.
    e.WebRequestExecutor.RequestHeaders.Add(
    "X-FORMS_BASED_AUTH_ACCEPTED", "f");
    }
    catch (Exception ex)
    {
    MessageBox.Show("Error setting authentication header: " + ex.Message);
    }
    }

    Thanks


    Tuesday, January 22, 2019 4:55 AM

Answers

  • Hi,

    For Windows authentication uses NetworkCredential class to authenticate.

    ClientContext context = new ClientContext("http://sp2010");
    context.Credentials = new NetworkCredential("username", "pwd", "domain");

    For Forms based authentication, uses like below.

    ClientContext ctx = new ClientContext("http://sp2010"); 
    ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; 
    ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("myUserName", "myUserPwd");

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Praveenrangu Tuesday, January 22, 2019 3:21 PM
    Tuesday, January 22, 2019 8:59 AM
    Moderator

All replies

  • Hi

    You can use Fiddler to check why 401 got.

    Here is a reference for you.

    https://piasys.com/blog/accessing-sharepoint-2010-via-csom-using-claims-based-authentication-and-providing-fedauth-cookie/


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Please Vote and Mark as Answer if it helps you.

    Tuesday, January 22, 2019 5:27 AM
  • Hi Praveenrangu,

    First Check Columns and List name is correct and match with the Internal Name


    This could be due to not having safe update false in your SharePoint site,

    Try to update this in SharePoint PowerShell 


    $w = Get-SPWeb http://gauti.sharepoint.com/Testsubsite
    $w.AllowUnsafeUpdates = true;



    Melick Rajee http://melick-rajee.blogspot.com

    Tuesday, January 22, 2019 5:43 AM
  • i checked the internal names as well.Iam passing column names and list names fine no luck still iam facing same issue.
    Tuesday, January 22, 2019 6:02 AM
  • Hi,

    For Windows authentication uses NetworkCredential class to authenticate.

    ClientContext context = new ClientContext("http://sp2010");
    context.Credentials = new NetworkCredential("username", "pwd", "domain");

    For Forms based authentication, uses like below.

    ClientContext ctx = new ClientContext("http://sp2010"); 
    ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; 
    ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("myUserName", "myUserPwd");

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Praveenrangu Tuesday, January 22, 2019 3:21 PM
    Tuesday, January 22, 2019 8:59 AM
    Moderator