Skip to main content
Solved

401 Error while using API to add members to group


Forum|alt.badge.img+2

We have a .Net C# application that runs on a Windows 2012 server and is attempting to add members to a static group via a PUT command and is throwing a 401 Unautorized error for each operation. The error is as follows:

2016-08-01 18:17:12,302 [ERROR] (PROGRAM.Helpers.JssHttpHelper) WebException at 'https://testdomain.com:8443/JSSResource/computergroups/id/18'. Exception: 

System.Net.WebException: The remote server returned an error: (401) Unauthorized.

The code is question is:

using System;
using System.Text;
using System.Net;
using System.Net.Security;
using System.IO;
using log4net;
using System.Reflection;

namespace Program.Helpers
{
    public static class JssHttpHelper
    {
        private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static void PutXml(string jssUrl, string username, string password, string url, string xml)
        {
            var credentialCache = new CredentialCache();
            credentialCache.Add(new Uri(jssUrl), "Basic", new NetworkCredential(username, password));

            HttpWebResponse response = null;
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "PUT";
                request.ContentType = "text/xml";
                request.Credentials = credentialCache;
                request.KeepAlive = true;
                request.ContentLength = xml.Length;

                ServicePointManager.ServerCertificateValidationCallback =
                    new RemoteCertificateValidationCallback((a, b, c, d) => { return true; });

                using (Stream stream = request.GetRequestStream())
                {
                    UTF8Encoding encoding = new UTF8Encoding();
                    byte[] bytes = encoding.GetBytes(xml);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Close();
                }

                using (response = (HttpWebResponse)request.GetResponse())
                {
                    if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK)
                    {
                        _log.Info(string.Format("Successfully sent a put request to '{0}'.", url));
                        return;
                    }
                    else
                    {
                        throw new Exception(string.Format("Response Failure: HttpStatus {0} - {1}", response.StatusCode, response.StatusDescription));
                    }
                }
            }
            catch (WebException ex)
            {
                _log.Error(string.Format("WebException at '{0}'. Exception: {1}", url, ex.ToString()));

                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    response = ex.Response as HttpWebResponse;
                    if (response != null)
                    {
                        _log.Info(string.Format("WebExceptionStatus.ProtocolError at '{0}'. HttpWebResponse: '{1}' ", url, response.ToString()));
                    }
                }
                throw;
            }
            catch (Exception ex)
            {
                _log.Error(string.Format("Exception at '{0}'. Exception: {1}", url, ex.ToString()));
                throw;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }
    }
}

Has anyone experienced the 401 error while attempting to add members to a group or a similar operation? Thank you for your consideration.

Best answer by dwandro92

Does the authenticating user have "Update" permissions for Static User Groups?

View original
Did this topic help you find an answer to your question?

3 replies

Forum|alt.badge.img+9
  • Contributor
  • 125 replies
  • Answer
  • August 2, 2016

Does the authenticating user have "Update" permissions for Static User Groups?


donmontalvo
Forum|alt.badge.img+36
  • Legendary Contributor
  • 4293 replies
  • August 2, 2016

@dwandro92 Richard might owe you some GREEN (hit "helpful"), that did the trick! :)


Forum|alt.badge.img+2
  • Author
  • New Contributor
  • 1 reply
  • August 2, 2016

Thanks dwandro92 - that was the issue. Much appreciated!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings