简体   繁体   中英

Digest authentication returning unauthorized

I am trying to authenticate through Digest authentication and the API is returning Unauthorized and when I check the count on cookies I can see cookies are not being passed to the actual request. However, I got it to work via postman by getting the cookies first and add it and make a request. But, when I do it via the code I am getting unauthorized.

Please guide me on how I can authenticate using digest authentication type.

This method get the cookies

     private RestClient _Client = new RestClient();
     public API()
     {
        _Client = new RestClient(_BaseUrl);
        _Client.Authenticator = _Creds;
        if (_SessionEnabled)
        {
            var request = new RestRequest("admin/getsession", Method.GET);

            IRestResponse response = _Client.Execute(request);
            var content = response.Content;

            _Cookie = response.Cookies;
            SCHelper.CheckLinOTPResponse(response);
            
        }
        

Adding a session to the actual request

   public static RestRequest AddSessionToRequest(RestRequest Request, IList<RestResponseCookie> Cookie, bool SessionEnabled)
    {
        if(SessionEnabled)
        {
            Request.AddQueryParameter("session", Cookie[0].Value);
            Request.AddParameter(Cookie[0].Name, Cookie[0].Value, ParameterType.Cookie);
        }            
        return Request;
    }

This is the method that generates OTP by first authenticating using the Digest authentication mechanism. It fails here and shows unauthorized.

   public string GenerateOtp(string Serial, string User, string Realm)
    {
        try
        {
            string username = "";
            string password = "";

            var client = _Client;
            var credentials = username + ":" + password;
            var base64Credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
            var request = new RestRequest("gettoken/getotp", Method.GET);
            //client.Authenticator = new HttpBasicAuthenticator(username, password); //tried to add this and it didn't work
            request.AddHeader("Authorization", "Digest " + base64Credentials);

            //check session
            request = SCHelper.AddSessionToRequest(request, _Cookie, _SessionEnabled);
            request.AddParameter("user", User, ParameterType.QueryString);
            request.AddParameter("realm", "myRealm", ParameterType.QueryString);
            request.AddParameter("serial", Serial, ParameterType.QueryString);

            IRestResponse response = client.Execute(request);  //This is were it fails.
            SCHelper.CheckLinOTPResponse(response);
            var content = response.Content;
            return content;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

I have finally come up with the solution are some battles. I had to add the headers and get cookies from the actual RestRequest call and it connected successfully.

        request.AddHeader("Accept", "application/json");
        request.Parameters.Clear();
        request.AddParameter("application/json", request, ParameterType.RequestBody);
        
        request.AddParameter(_Cookie[0].Name, _Cookie[0].Value, ParameterType.Cookie);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM