Warning CS0618 'LoginSettings.NetworkCredential' is obsolete: 'From 2022 R1

I am using Config API client for getting camera details from milestone. Now I updated SDK to 22.3. I got the following warning,

Severity Code Description Project File Line Suppression State

Warning CS0618 ‘LoginSettings.NetworkCredential’ is obsolete: ‘From 2022 R1, this property may be null when the user is using OAuth 2.0 tokens. Try always to use the LoginSettings class and check the IsOAuthIdentity == false, before using the NetworkCredential’ ConfigAPIClient

I checked ConfigAPI Client sample code, there warning is disabled using following code

#pragma warning disable CS0618 // Type or member is obsolete
            if (loginSettings != null)
            {
                if (basic)
                {
                    channel.Credentials.UserName.UserName = "[BASIC]\\" + loginSettings.NetworkCredential.UserName;
                    channel.Credentials.UserName.Password = loginSettings.NetworkCredential.Password;
                    channel.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
                    {
                        CertificateValidationMode = X509CertificateValidationMode.None,
                    };
                }
                else
                {
                    channel.Credentials.Windows.ClientCredential.UserName = loginSettings.NetworkCredential.UserName;
                    channel.Credentials.Windows.ClientCredential.Password = loginSettings.NetworkCredential.Password;
                }
            }
#pragma warning restore CS0618 // Type or member is obsolete

Why this is disabled? What is the fix for this warning?

Hey Mathew

The method where this is found in is called CreateClientProxy, it is only used if the server does not support OAuth authentication. Servers from 21R1 support OAuth, if you need to connect to older servers, then you have to include the [pragma](javascript:void(0); “pragma”) tag (unless you accept to have a warning when you build). To see how to create a OAuth client proxy, see the method “CreateOAuthClientProxy”:

private IConfigurationService CreateOAuthClientProxy(string address, int serverPort, LoginSettings loginSettings)
        {
            // If the OAuth server is available it uses OAuth version of ServerCommandService.
            var uri = ConfigApiServiceOAuthHelper.CalculateServiceUrl(address, serverPort);
            var oauthBinding = ConfigApiServiceOAuthHelper.GetOAuthBinding(serverPort == 443);
            string spn = SpnFactory.GetSpn(uri);
            EndpointAddress endpointAddress = new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity(spn));
 
            ChannelFactory<IConfigurationService> channel = new ChannelFactory<IConfigurationService>(oauthBinding, endpointAddress);
            channel.Endpoint.EndpointBehaviors.Add(new AddTokenBehavior(loginSettings.IdentityTokenCache.Token));
            ConfigApiServiceOAuthHelper.ConfigureEndpoint(channel.Endpoint);
 
            return channel.CreateChannel();
        }