AutoGRAPH.NET Service Auth (EN)

Материал из TK
Перейти к: навигация, поиск

Authentication process

Description Without authentication With authentication
authentication not required and not used required

Before calling all other methods you need to call Login method for get access token.

In all other methods you need to be use this token.

Schema location

In directory common-directory

Schemas located in one directory and shared between all clients

In directory for each user (for example {users-directory}\username1\Download - directory structure from AutoGRAPH Server "Users" directory and need to containt Schemes, Geofences, Devices, ... subdirectories like AutoGRAPH.Pro).

Users don't have access to schemas of other users.

Check of device accessibility All devices will available to all clients (restricted only by AutoGRAPH Server key) Client can access only to specified devices, granted by server administrator
Data directory common

common

Authentication

Authentication is executed by calling Login method. Returned access token must be passed to HTTP header for all other requests to service.

WCF (Windows Communication Foundation)

For WCF protocol you need to be use WCF behavior extensions and message inspectors for injecting access token. Example:

var svc = new AutoGRAPHSvcClient();
var bh = new AuthenticateEndpointBehavior();
svc.Endpoint.Behaviors.Add(bh);
bh.Token = svc.Login("demo", "demo");
var devices = svc.EnumDevices("DemoSchema");
var geofences = svc.EnumGeofences("DemoSchema");

Next, you can use all methods of proxy-class as usual. Additional file with helper classes included in examples archive.

JSON

For JSON protocol you need to be HTTP header AG-TOKEN.

WebClient client = new WebClient();
client.Headers["Content-Type"] = "application/json";
client.Headers["AG-TOKEN"] = client.UploadString(
   "http://localhost:8300/Login",
   "{\"UserName\":\"demo\",\"Password\":\"demo\"}").Trim('\"', '\"');

// Next, you can use this WebClient instance for working with service methods:

client.DownloadString("http://localhost:8300/EnumSchemas");

VBA example (Microsoft Excel):

Sub WebClient()
Dim iToken As String
Dim URL As String
LoginURL = "http://95.213.159.8:8300/Login"
URL = "http://95.213.159.8:8300/"
iToken = GetHTTPResponseToken(LoginURL)
UserForm1.Label1.Caption = GetHTTPResponse(URL, iToken, "EnumDevices/Demo")
UserForm1.Show
End Sub
Function GetHTTPResponseToken(ByVal sURL As String) As String
    Dim logpass As String
    logpass = "{""UserName"":""Demo"",""Password"":""Demo""}"
    On Error Resume Next
    Set client = CreateObject("MSXML2.XMLHTTP")
    With client
      .Open "POST", sURL, False
      .SetRequestHeader "Content-Type", "application/json"
      .send [logpass]
       GetHTTPResponseToken = .ResponseText
       GetHTTPResponseToken = Mid(GetHTTPResponseToken, 2, Len(GetHTTPResponseToken) - 2)
    End With
    Set client = Nothing
End Function
Function GetHTTPResponse(ByVal sURL As String, Token As String, Metod As String)
    Dim logpass As String
    On Error Resume Next
    Set client = CreateObject("MSXML2.XMLHTTP")
    With client
      .Open "GET", sURL & Metod, False
      .SetRequestHeader "Content-Type", "application/json"
      .SetRequestHeader "AG-TOKEN", Token
      .send
       GetHTTPResponse = .ResponseText
    End With
    Set client = Nothing
End Function