I have tried every blog I could find about it . But I am not able to resolve status 401 with NTLM challenge.
So basically I need to get profile picture of an user which is hosted in company server.
When I hit the url in browser It prompts for username and password ,once I provide it I get the image.
But same I am not able to achieve in Xamarin.iOS
Below is the code snippet I am trying with .
var credentials = new NetworkCredential("myUserName", "Password", "domain");
var handler = new HttpClientHandler { Credentials = credentials, UseDefaultCredentials = false };
handler.AllowAutoRedirect = false;
var client = new HttpClient(handler);
try
{
var response = await client.GetAsync("https://mycustomUrl.net/User%20Photos/Profile%20Pictures/eurn_martin_kremmer_MThumb.jpg");
Console.WriteLine(response);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Response I am getting is
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: Microsoft-IIS/8.0
SPRequestGuid: 5725369e-b047-40c5-d468-0417992ffab3
request-id: 5725369e-b047-40c5-d468-0417992ffab3
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 2
SPIisLatency: 0
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 15.0.0.4779
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Date: Wed, 13 Dec 2017 13:55:26 GMT
Content-Length: 0
}
Wow , I didn't know the solution is as easy as pie , when you throw the same request with NSURLConnection.
here,
responseData = new NSMutableData();
request = new NSMutableUrlRequest(uri,NSUrlRequestCachePolicy.UseProtocolCachePolicy, 6000);
request.HttpMethod = "GET";
connection = new NSUrlConnection(request, this);
This works without hassle in Citrix MAM environment.
But to test it in simulator you need to handle challenge like below.
public override void WillSendRequestForAuthenticationChallenge(NSUrlConnection connection, NSUrlAuthenticationChallenge challenge)
{
Console.WriteLine(challenge.ProtectionSpace);
if (challenge.PreviousFailureCount == 0 )
{
NSUrlCredential cred = new NSUrlCredential(this.myUserName, this.myPass, NSUrlCredentialPersistence.None);
challenge.Sender.UseCredential(cred, challenge);
}
}
Related
Okay, so I'm very new to using API's in code and I've been able to use a few that were actually pretty easy. But none of them required authentication. I've been trying to use Jira's REST API service via C#'s HttpClient class. See code below:
public void UpdateJiraIssue(string issueValue)
{
string url = $#"http://jira.mySite.com/rest/api/2/issue/{issueValue}/editmeta";
string jsonString = #"myNeatJsonData";
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
//Initialize Client
HttpClient apiClient = new HttpClient();
apiClient.BaseAddress = new System.Uri(url);
apiClient.DefaultRequestHeaders.Accept.Clear();
byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
apiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
async Task RunJiraAPI()
{
using (HttpResponseMessage resp = await apiClient.PostAsync("editmeta", content))
{
if (resp.IsSuccessStatusCode)
{
var jsonSring = await resp.Content.ReadAsStringAsync();
}
}
}
RunJiraAPI();
return;
}
The problem I run into is that I get a 401 error (Authentication). Here's what my 'resp' object contains when I run the code:
resp: {StatusCode: 401, ReasonPhrase: ' ', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
X-AREQUESTID: 400x1314x1
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
X-ASEN: SEN-11158344
X-AUSERNAME: anonymous
Cache-Control: no-store, no-transform, no-cache
Set-Cookie: atlassian.xsrf.token=B2ZY-C2JQ-1AGH-PBLW_5ccc79da5af8e6abcb9bff5250f3305af3b2877a_lout; Path=/; Secure
WWW-Authenticate: OAuth realm="https%3A%2F%2Fjira.mySite.com"
X-Powered-By: ARR/3.0
X-Powered-By: ASP.NET
Date: Wed, 15 Jan 2020 13:40:22 GMT
Content-Length: 109
Content-Type: application/json; charset=UTF-8
}}
Request Message: {Method: POST, RequestUri: 'https://jira.rhlan.com/rest/api/2/issue/RHD-1116/editmeta', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Authorization: Basic cWE6aGVjc29mdDEyMw==
Accept: application/json
Content-Type: Application/json; charset=utf-8
Content-Length: 70
}}
Status Code: Unauthorized
I need to work on my json string a bit to get it working right (which is why I didn't include what it actually contains), but once I get passed the authentication error, I'll probably actually change things to do a get Jira issue via the API so I can see all the json data returned that way. Then I'll edit my json string accordingly.
Any ideas what I'm doing wrong here?
You can pass in credentials assuming you have a username and an api token.
string credentials= string.Format("{0}:{1}", username, apitoken);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(credentials);
And in your apiClient you can use it like this.
apiClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteCredentials));
You need a username and api-token. Your api-token should be your login password.
I am trying to make SignalR Client work from C# code but I couldn't. I got this error:
"StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Transfer-Encoding: chunked
Access-Control-Allow-Origin: *
Cache-Control: private
Date: Tue, 30 Jul 2019 11:54:07 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Type: text/html; charset=utf-8
}"
But it works fine from jQuery. This is my code:
I tried this tutorial : https://programmer.group/signalr-hubs-api-details-net-c-client.html and also at the server side this my startup file code
var config = new HubConfiguration();
config.EnableDetailedErrors = true;
config.EnableJSONP = true;
app.MapSignalR(config);
And this in the Client side:
async void ConnectToSignlr()
{
try
{
HubConnection hubConnection = new HubConnection("http://localhost:8087/");
hubConnection.Credentials = System.Net.CredentialCache.DefaultCredentials;
hubConnection.TraceLevel = TraceLevels.All;
hubConnection.TraceWriter = Console.Out;
IHubProxy proxy = hubConnection.CreateHubProxy("ChatHub");
proxy.On<string>("chekroomstauts", (message) =>
{
//do something on your ui maybe?
Response.Write("hello you connected");
});
proxy.On("notify", (message) =>
{
Response.Write("hello you notify");
}
);
// connect to hup pro
await hubConnection.Start();
await proxy.Invoke("Join", "1000");
}
catch (AggregateException ex)
{
foreach (var errInner in ex.InnerExceptions)
{
//Debug.WriteLine(errInner); //this will call ToString() on the inner execption and get you message, stacktrace and you could perhaps drill down further into the inner exception of it if necessary
Response.Write(errInner + "</br>");
}
}
}
Without the exception detials I can only guess.
Maybe "chekroomstauts" is a typo and your server wants to call "checkroomstatus"?
Maybe "Join" is not defined?
Kind regards
Bernd
I'm getting a 401 error whenever I attempt to get a response from HttpClient when I turn off Anonymous Authentication.
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1,
Content: System.Net.Http.StreamContent, Headers:
{
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcUGFycmlzaFxEb2N1bWVudHNcJ0xSXFNvdXJjZVxBcHBzXExSUiBBRkVcTFJSX0FGLVxhcGlcYWZlXDg0QTk0NjVFQzg2QTQwQjNBNEJCNkJDOTI3MTFGRjNB?=
Cache-Control: private
Server: Microsoft-IIS/10.0
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Mon, 27 Aug 2018 18:14:05 GMT
Content-Length: 6166
Content-Type: text/html; charset=utf-8
}
For testing I have the controller Authenticating on a single user.
[Authorize(Users = #"Domain\Username")]
public class ExampleController : Controller
{
public async Task<ActionResult> Index(string exampleId)
{
var baseUrl = $"{Request.Url.Scheme}://{Request.Url.Authority}{Url.Content("~")}";
var client = new HttpClient(); // <-- This is WRONG*
var response = await client.GetAsync($"{baseUrl}api/example/{exampleId}");
var example = await response.Content.ReadAsAsync<ExampleModel>();
return View(example);
}
}
I've looked into using HttpClient.DefaultRequestHeaders.Authentication, but I don't see a way to get it to work with ActiveDirectory. I've looked here but can't understand where I'd be getting an authorization token in this case.
If I create a new controller manually and invoke it directly instead of through the API this works, but it really isn't authenticating anything in that case right?
*Edit:
As ADyson points out, my error was not initializing the HttpClient correctly. Here is how it should have been written:
var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
You need to set UseDefaultCredentials = true (in an instance of HttpClientHandler, which you pass to the HttpClient).
I am having a big problem when trying to authenticate to QuickBlox's server using a Token.
The method I use is:
public static async Task<LoginResponse> GetLoginResponseAsync(string email, string password)
{
LoginResponse result = null;
using (var client = new HttpClient())
{
string token = QbProvider.SessionResponse.Session.Token;
LoginRequest request = new LoginRequest()
{
Email = email,
Password = password
};
using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, LoginUrlRequest))
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("QB-Token", token);
using (var response = await client.SendAsync(requestMessage))
{
string json = response.Content.ReadAsStringAsync().Result;
result = JsonConvert.DeserializeObject<LoginResponse>(json);
}
}
}
return result;
}
The server's response is:
{"errors":["Token is required"]}
And the headers (debugging) in the client object are:
{
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Date: Thu, 01 Jun 2017 12:05:29 GMT
QuickBlox-REST-API-Version: 0.1.1
Server: openresty/1.9.15.1
Status: 401 Unauthorized
Strict-Transport-Security: max-age=31536000
WWW-Authenticate: OAuth realm=users
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: 584a0dca-fc44-4114-9626-327ac1729f67
X-Runtime: 0.003430
X-XSS-Protection: 1; mode=block
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Content-Length: 32
}
}
When I use the Token in Postman, the server's response is successfull.
Do you know what am I doing wrong?
Thank you so much in advance!
Regards.
Try adding your token header using requestMessage.Headers.Add("QB-Token", token) instead of your Authorization one – by #dukedukes
So I'm trying pass user data from one controller to another. I've been reading a lot about it here ---> http://www.dotnet-tricks.com/Tutorial/webapi/F2aL081113-Passing-multiple-complex-type-parameters-to-ASP.NET-Web-API.html and this guy seems to be doing exactly what I want to do, and he's using .PostAsJsonAsync to do it.
This is controller 1, which takes in user data and basically re-routes it to the other controller for authentication.
public ActionResult IDB(IDBUser i)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:64819");
HttpResponseMessage result = client.PostAsJsonAsync("/api/Participant/Authenticate", i).Result;
if (result.IsSuccessStatusCode)
{
return View();
}
else { // hits this breakpoint with 500 server error
return View("Index");
}
}
}
}
This seems to successfully make it to the other controller (below), as I'm getting a 500 error. However, I've got a breakpoint right at the beginning of the method, and it never seems to hit it.
[System.Web.Http.ActionName("Authenticate")]
[System.Web.Http.HttpPost]
public HttpResponseMessage Authenticate(IDBUser u)
{ //breakpoint it neve hits is on this line
bool Authenticate = false;
CredentialTester ct = new CredentialTester(u.password, u.username);
bool isAuthenticatedInt = ct.IntTest();
bool isAuthenticatedAcp = ct.AcpTest();
if (isAuthenticatedInt == true || isAuthenticatedAcp == true)
{
Authenticate = true;
return Request.CreateResponse(HttpStatusCode.OK, Authenticate);
}
else Authenticate = false;
return Request.CreateResponse(HttpStatusCode.NotAcceptable);
}
Any help is apprecitaed. Thanks everyone!
UPDATE
: I was able to look at the error and it is listed here:
{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Pragma: no-cache X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcYTU4NDYxOVxEb2N1bWVudHNcYXAxMDk1MTQtcWEtYXVvdG1hdGlvblxXaVFhLlZhbC1JZFxNVkNcV2lRYS5WYWwtSWQuV2ViU2VydmljZVxXaVFhLlZhbC1JZC5XZWJTZXJ2aWNlXFdpUWEuVmFsLUlkLldlYlNlcnZpY2VcYXBpXFBhcnRpY2lwYW50XEF1dGhlbnRpY2F0ZQ==?= Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Content-Type, Accept Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Cache-Control: no-cache Date: Thu, 12 May 2016 17:07:39 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Length: 2307 Content-Type: application/json; charset=utf-8 Expires: -1}}
it looks like there's a problem with my Access- Control-Origin. I've put the appropriate adjustments in my web.config file (I've had a similar error before) but no luck.