# POST /LogOn/

Logs the user on using standard log on options.

## Request

Method | Request URI
--- | ---
POST | /API/LogOn/

### Request Body

Name: options
Type: Dundas.BI.WebApi.Models.LogOnOptions

## Response

### Response Body

A Dundas.BI.WebApi.Models.LogOnResultData object, or a status code indicating the problem.

## Examples

This example makes a rest call and gets a session ID.

C# Java JavaScript

    using System.Net;
    using System.Net.Http;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Web.Script.Serialization;

       ...

    void Main()
    {
    	using(HttpClient httpClient = new HttpClient())
    	{
    		string sessionId =
    			GetSessionId(
    				httpClient,
    				"http://localhost:8005/",
    				"admin",
    				"1234"
    				);
    	}
    }

    private static string GetSessionId(
    	HttpClient client,
    	string baseUri,
    	string username,
    	string password
    )
    {

        string logonUri = baseUri + "Api/LogOn/";
        string result = string.Empty;

        var logonOptions = new
        {
            accountName = username,
            password = password,
            cultureName = string.Empty,
            deleteOtherSessions = false,
            isWindowsLogOn = false

        };

    	JavaScriptSerializer serializer = new JavaScriptSerializer();
    	var requestBodyAsString = serializer.Serialize(logonOptions);

    	StringContent content =
    		new StringContent(
    			requestBodyAsString,
    			Encoding.UTF8,
    			"application/json"
    			);

        using (var response = client.PostAsync(logonUri, content).Result)
        {
            if (response.IsSuccessStatusCode)
            {
                string jsonString =
    				response.Content.ReadAsStringAsync().Result;

    			var obj = (Dictionary<string,object>) serializer.DeserializeObject(jsonString);

    			if(obj["logOnFailureReason"].ToString() == "None")
    			{
    				result = obj["sessionId"].ToString();
    			}
    			else
    			{
    				throw new Exception("Login failed");
    			}

            }

        }

     	return result;
    }

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.entity.StringEntity;
    import org.json.JSONObject;

       ...

    public static void main(String[] args)
    {
       String sessionId =
          GetSessionId(
             "http://localhost:8005/",
             "admin",
             "1234"
          );

       System.out.println("SessionID: " + sessionId);
    }

    private static String GetSessionId(String baseUri, String username, String password)
    {
       String result = "";

       try
       {

          String logonUri = baseUri + "Api/LogOn/";

          HttpClient httpClient = HttpClientBuilder.create().build();

          HttpPost httpPost = new HttpPost(logonUri);

    			StringEntity input =
            new StringEntity(
    					"{"
    					+ "\"accountName\":\"admin\","
    					+ "\"password\":\"1234\",\"cultureName\":\"\","
    					+ "\"deleteOtherSessions\":false,"
    					+ "\"isWindowsLogOn\":false"
    					+ "}"
            );

          httpPost.setEntity(input);

          HttpResponse response =
             httpClient.execute(httpPost);

          String json = EntityUtils.toString(response.getEntity());
          JSONObject jsonObject = new JSONObject(json);

          if(jsonObject.getString("logOnFailureReason").equals("None"))
          {
             result = jsonObject.getString("sessionId");
          }

       }
       catch (Exception e)
       {
          e.printStackTrace();
       }

       return result;
    }

    var logonOptions =
    {
       accountName: 'admin',
       password: '1234',
       cultureName: 'en-us',
       deleteOtherSessions: false,
       isWindowsLogOn: false
    };

    $.ajax({
       type: "POST",
       url: "http://localhost:8005/Api/LogOn/",
       contentType: "application/json",
       data: JSON.stringify(logonOptions),
       success: function(data) { alert(data.sessionId); },
       error: function(data) { alert('failed'); }
    });

This example makes a rest call and gets a session ID using Windows Authentication.

C#

    using System.Net;
    using System.Net.Http;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;
    using System.Web.Script.Serialization;

       ...

    void Main()
    {
    	HttpClientHandler handler = new HttpClientHandler();
    			handler.UseDefaultCredentials = true;

        using(HttpClient httpClient = new HttpClient(handler))
        {

            string sessionId =
                GetSessionId(
                    httpClient,
                    "http://localhost:8005/"
                    );

        }
    }

    private static string GetSessionId(
        HttpClient client,
        string baseUri)
    {

        string logonUri = baseUri + "Api/LogOn/";
        string result = string.Empty;

        var logonOptions = new
        {

            cultureName = string.Empty,
            deleteOtherSessions = false,
            isWindowsLogOn = true

        };

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        var requestBodyAsString = serializer.Serialize(logonOptions);

        StringContent content =
            new StringContent(
                requestBodyAsString,
                Encoding.UTF8,
                "application/json"
                );

        using (var response = client.PostAsync(logonUri, content).Result)
        {
    		response.Content.ReadAsStringAsync().Result.Dump();

            if (response.IsSuccessStatusCode)
            {
                string jsonString =
                    response.Content.ReadAsStringAsync().Result;

                var obj = (Dictionary<string,object>) serializer.DeserializeObject(jsonString);

                if(obj["logOnFailureReason"].ToString() == "None")
                {
                    result = obj["sessionId"].ToString();
                }
                else
                {
                    throw new Exception("Login failed");
                }

            }

        }

        return result;
    }

© 1999-2021 Dundas Data Visualization, Inc.