What is the best way in .Net to handle authentication & authorization protocols for downloading a file via https

David Thielen 3,116 Reputation points
2020-12-27T16:17:44.813+00:00

Hi all;

We need to implement pretty much every common authentication & authorization protocol for both reading files and OData queries from a url via https. We need this for both .Net Framework & .Net Core.

Is there a way to do this generically in the .Net runtime? Or is there a listing somewhere of how to call .Net to get this.

Or any other ways to solve this problem?

thanks - dave

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,159 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jerry Cai-MSFT 991 Reputation points
    2020-12-28T09:56:50.08+00:00

    Hi,DavidThielen-2188

    You can use WebClient.DownloadFile() to download the file from url and set authentication at the same time, like:
    DownloadFileFromUrl

    WebClient.DownloadFile

    using (System.Net.WebClient wc = new System.Net.WebClient())  
    {  
    	wc.Headers.Add("Cookie: Authentication=user"); // add a cookie header to the request  
    	try  
    	{  
    		string filename = System.Guid.NewGuid().ToString("N"); // use global unique identifier for file name to avoid conflicts  
    		wc.DownloadFile("http://www.prowaretech.com/", "C:\\files\\" + filename); // could add file extension here  
    		// do something with file  
    	}  
    	catch (System.Exception ex)  
    	{  
    		// check exception object for the error  
    	}  
    }  
    

    Best Regards,
    Jerry Cai


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.