How to authenticate from custom tools

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.2
  • Enterprise
  • Community
  • Planned

Overview

Sense/Net supports multiple authentication methods. When you want to access ECM functionality via OData REST API you will have to send authenticated requests to Sense/Net ECMS. This article gives you examples for using different authentication methods.

Details

Sense/Net provides three different types of authentications: Forms, Basic, NTLM. Here we give you example implementation for sending an OData request to Sense/Net ECMS using Basic and NTLM authentication methods.

Using Basic authentication

This could be a good choice if your site is configured to use Anonymous/Forms authentication.

Bear in mind that Basic authentication is not considered a secure method unless it is used via https.

The following console application sends a request to a Sense/Net installation running on localhost, and retrieves the site list using an OData request. Response is saved to a file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
 
namespace ODataTest
{
    class Program
    {
        static void WriteToFile(string filename, string s)
        {
            using (var fileStream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (var writer = new StreamWriter(fileStream))
                {
                    writer.Write(s);
                }
            }
        }
 
        static void SetBasicAuthForRequest(WebRequest myReq, Uri uri)
        {
            var username = "Builtin\\admin";
            var password = "admin";
 
            // if Basic is enabled in IIS
            //var mycache = new CredentialCache();
            //mycache.Add(uri, "Basic", new NetworkCredential(username, password));
            //myReq.Credentials = mycache;
 
            // if Basic is not enabled in IIS
            var usernamePassword = username + ":" + password;
            myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
        }
 
        static void SendRequest()
        {
            var path = "http://localhost/OData.svc/Root/Sites";
            var uri = new Uri(path);
            var myReq = WebRequest.Create(uri);
 
            SetBasicAuthForRequest(myReq, uri);
 
            try
            {
                var wr = myReq.GetResponse();
                using (var stream = wr.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var s = reader.ReadToEnd();
                        WriteToFile("respBasic.txt", s);
                    }
                }
            }
            catch (WebException wex)
            {
                WriteToFile("respBasic.txt", wex.Message);
            }
        }
 
        static void Main(string[] args)
        {
            SendRequest();
        }
    }
}

Note that we used the myReq.Headers.Add method instead of using the myReq.Credentials, since it will work even if Basic authentication is not explicitely configured in IIS.

Using NTLM authentication

This could be a good choice if your site is configured to use Windows authentication.

The following console application sends a request to a Sense/Net installation running on localhost, and retrieves the site list using an OData request. Response is saved to a file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
 
namespace ODataTest
{
    class Program
    {
        static void WriteToFile(string filename, string s)
        {
            using (var fileStream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (var writer = new StreamWriter(fileStream))
                {
                    writer.Write(s);
                }
            }
        }
 
        static void SetNTLMAuthForRequest(WebRequest myReq)
        {
            // use current user credentials
            myReq.Credentials = CredentialCache.DefaultCredentials;
 
            // use custom credentials
            //myReq.Credentials = new NetworkCredential { Domain = "DOMAIN", UserName = "windowsusername", Password = "windowsuserpassword" };
        }
 
        static void SendRequest()
        {
            var path = "http://localhost/OData.svc/Root/Sites";
            var uri = new Uri(path);
            var myReq = WebRequest.Create(uri);
 
            SetNTLMAuthForRequest(myReq);
 
            try
            {
                var wr = myReq.GetResponse();
                using (var stream = wr.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var s = reader.ReadToEnd();
                        WriteToFile("respNTLM.txt", s);
                    }
                }
            }
            catch (WebException wex)
            {
                WriteToFile("respNTLM.txt", wex.Message);
            }
        }
 
        static void Main(string[] args)
        {
            SendRequest();
        }
    }
}

Related links

References

There are no external references for this article.