Posted on 08-02-2016 06:08 AM
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.
Solved! Go to Solution.
Posted on 08-02-2016 07:06 AM
Does the authenticating user have "Update" permissions for Static User Groups?
Posted on 08-02-2016 07:06 AM
Does the authenticating user have "Update" permissions for Static User Groups?
Posted on 08-02-2016 08:58 AM
@dwandro92 Richard might owe you some GREEN (hit "helpful"), that did the trick! :)
Posted on 08-02-2016 11:39 AM
Thanks dwandro92 - that was the issue. Much appreciated!