简体   繁体   中英

401(unauthorised) while making service call

Getting 401(authorised) while making web api controller call

public bool CheckCarrierSCAC(int carrierID)
        {
            bool carrierScacSatus = false;
            carrierSCAC = new BackOfficeViewController().GetSCACCodeBYCarrierID(carrierID);
            logger.LogMessage(message: string.Format("Credentials {0}{1}", ConfigurationManager.AppSettings["HermesUserName"], ConfigurationManager.AppSettings["HermesPassword"]), logDate: true);
            Http.Preauthenticate = true;
            string serviceUrl = string.Format("{0}/CarrierSCAC?carrier={1}", ConfigurationManager.AppSettings["GatewayInterface"], carrierSCAC);
            logger.LogMessage(message: string.Format("Check Carrier SCAC Service URL {0} ", serviceUrl), logDate: true);
            try
            {
                carrierScacSatus = Http.Get<bool>(uri: serviceUrl, cookieContainer: null, contentType: "application/json");
            }
            catch (Exception exception)
            {
                logger.LogException(exception, message: "error while check Carrier Scac =" + exception.Message);
            }
            return carrierScacSatus;
        }

I have already used preauthentication still getting same error

Setting Http.Preauthenticate = true just tells the web request to send the Authorization header to that Uri going forward, assuming it's a pass-through to .NET's HttpWebRequest.Preauthenticate . In this case, you don't appear to actually be providing any credentials to the web request. The only case where you reference the credentials is in the logger message.

The Http.Get<T> method should allow you to provide either raw Header values (in which case you'll need to add your own Authorization header) or the credentials so that it creates the header for you. (This appears to be a library that wraps the C# WebRequest or some similar connection library, so you'll need to check it's documentation for specific details).

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