webclient does not work anymore

MamagotchiMamagotchi Member ✭✭
edited March 2 in Xamarin.Android

Hi,

I used a simple Code to connect to SharePoint 2013 Foundation using webclient.

      private void Download_Sharepoint(string user_send, string pass_send, string url)
        {
            string fileName = Path.GetFileName(url);
            Uri uri = new Uri(url);

            WebClient webClient = new WebClient
            {
                Credentials = new NetworkCredential(user_send, pass_send, General_Class.Domain)
            };

            ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(WebClient_DownloadFileCompleted);
            webClient.DownloadFileAsync(uri, Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/" + fileName);
        }

Since last Monday i get the error 401 (only in android-app), but I changed Nothing at my app. Any idea?

Tagged:

Answers

  • axaaxa Member ✭✭✭
    edited March 4

    the fact that you are receiving a 401 error indicates that infact your webrequest is indeed working, and responding to you.
    401 is an authentication error, check your credentials still valid.

  • MamagotchiMamagotchi Member ✭✭
    To my mind my credentials are still valid. I tried it with different valid users, but I got the same error. The domain didn't change... Is there anything I can add, so I can see the specific reason which cause the error? "Error.Message" just generate a common message...
  • yelinzhyelinzh Member, Xamarin Team Xamurai

    What about the server ,does it receive an error message?

  • MamagotchiMamagotchi Member ✭✭

    @axa said:
    the fact that you are receiving a 401 error indicates that infact your webrequest is indeed working, and responding to you.
    401 is an authentication error, check your credentials still valid.

    The App with the same code is working fine on W10 (UWP). SO to my mind the credentials seems to be be valid.

    @yelinzh said:
    What about the server ,does it receive an error message?

    I will ask the division for IT in my company group. Thanks for the hint.

  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited March 8

    @Mamagotchi What‘s the result? Have you solved the issue?

  • MamagotchiMamagotchi Member ✭✭
    Unfortunately I have not solved the issue yet. But 401 is an error caused by the client. So I guess I have to code a little bit more "precisely". But userame, password und domain are valid.
  • yelinzhyelinzh Member, Xamarin Team Xamurai

    Have you tested the permissions?

  • MamagotchiMamagotchi Member ✭✭
    Yes. I gave my app every possible permission, but it still did not work. My next step is to get the suberror of 401. So I'm still waiting for answer of the IT-divison hosting the sharepoint...
  • MamagotchiMamagotchi Member ✭✭

    So I got some information:

    Non-OAuth request. IsAuthenticated=False, UserIdentityName=, ClaimsCount=0

    So SharePoint did not receivce my credentials (user, password, domain), did it?

  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited March 15

    You get the error only in android. Does it work well in ios?
    Try to check you url , it may also cause the problem you post.

  • MamagotchiMamagotchi Member ✭✭
    It works fine in windows (uwp) und it had worked fine on android 3 weeks ago. I checked the url (copy it from sharepoint). Nothing changed.

    I don't know why "UserIdentityName" is empty (this is from iis log). I checked my credentials, but username, password and domain are valid.
  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited March 15

    There is a link , it may help you.
    https://stackoverflow.com/questions/4220832/how-to-handle-basic-authentication-in-webview

    @Override
    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
        handler.proceed("username", "password");
    }
    
  • MamagotchiMamagotchi Member ✭✭

    Little update: I try to use HttpClient, but I get also "401 - Unauthorized". I use the NuGet "HttpLogger" to put the request and response in a label. I run my test-app on Windows and on Android in Debug-Mode. The results u can see in my 2 Screenshots.

    Sooo I'm a beginner and do not understand all of it. But it looks like uwp auth using some kind of system-login and android try to login using a web-login (WWW-Authenticate: NTLM). So how could I do this (NTML). Just find some codes using "Basic" and only "username+password" without domain.

  • axaaxa Member ✭✭✭
    edited March 16
    Might i suggest you get familiar with WireShark... it will allow you to see exactly the requests you are making as well as the responses...
    Then you can try to be sure your requests match in both applications...
    If they do then it makes a bit more sense to compare the responses....


    That said, you might check out this post:
    https://forums.xamarin.com/discussion/141092/restsharp-v-105-2-3-fails-to-read-response-of-apis-which-need-authentication

    There a user was sure they had an authentication issue when i suggested it might be a common problem with the client:
    https://github.com/restsharp/RestSharp/issues/1280

    Which at least might be a problem with mono:
    https://github.com/mono/mono/issues/9511

    No one seems to know, but it seems all of these problems manifest with a 401 authentication error which confuses and leads people to make all sorts of assumptions...

    But you already mentioned your UWP program works, im assuming that only works on windows platforms, while your Android using mono doesnt... why not start with what is fact.

    If you can catch a failed response, check if there is a handled exception, if so and it throws on a BufferBlock copy, then join the club...
  • MamagotchiMamagotchi Member ✭✭

    So I check my request. UWP using version 2.0 and Android is using version 1.1. Anything else is similar.

  • axaaxa Member ✭✭✭
    Sorry I don't follow, versions of?
  • MamagotchiMamagotchi Member ✭✭

    Version of HTTP. UWP using HTTP/2.0 and Android is using HTTP/1.1. So the request is quite equal.

  • axaaxa Member ✭✭✭
    > @Mamagotchi said:
    > Version of HTTP. UWP using HTTP/2.0 and Android is using HTTP/1.1. So the request is quite equal.

    Maybe something I don't understand but I would call that NOT quite equal.
    Regardless, I still recommend packet sniffing with a managed port. Actually seeing what gets sent and received is very helpful
  • MamagotchiMamagotchi Member ✭✭
    edited March 21

    Finally I got the IIS-Log from the division for IT. So here is, what I got:

    • error: 401
    • sub-error: 1
    • win32-error: 2148074254

    Win32-error " 401 1 2148074254" means "No credentials are available in the security package". In other words, the client has not sent any credentials. So I checked it in VS, but my credentials are set and valid...

    After this error I got this:

    • error: 401
    • sub-error: 1
    • win32-error: 2148074252

    Win32-error " 401 1 2148074252" means "credentials cannot be verified".

  • axaaxa Member ✭✭✭
    edited March 22

    So you check in VS debugger and your sure you send your credetials. I agree, usually that is enough but infact that is not actual proof to anyone even you that they are actually being sent.
    You will only be able to be sure exactly what is being sent and exactly what is being received by packet sniff using something like wireshark. Then you can capture the log, and send it back to IT with proof the correct credentials are in fact sent...

    Unless this is a secure session then it is a bit more work to packet capture.

  • MamagotchiMamagotchi Member ✭✭
    edited March 22

    I try to use Fiddler and WireShark, but it does not work with vpn on android...

    Edit: So I try to reproduce the error with UWP. I got the exatly same error with same iis-log, when I removed the credentials. So it looks like the credentials were not sent on Android.

  • MamagotchiMamagotchi Member ✭✭
    edited March 24

    Could someone try to run my code on android to check it? When the code runs, maybe it is a problem of SharePoint 2013 foundation which I am using as a server.

    Edit#1: UWP-App is using "ICredntials" and Android-App is using "Credentials". Could somebody explain the differences?

  • MamagotchiMamagotchi Member ✭✭

    Refering to https://www.innovation.ch/personal/ronald/ntlm.html it looks like my authorization stops after step#2, but I don't know why because I set Connection to "Keep-Alive"...

Sign In or Register to comment.