I have created a WCF appication that have access to an sql database. Some codes of the WCF are as below:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.ServiceModel
Imports System.Runtime.Serialization
Imports System.Security.Cryptography
Public Class Service1
Inherits System.Web.UI.Page
Implements IService1
<ServiceContract()>
Public Interface IService1
<OperationContract()>
Function [GetUser]() As UserData
<OperationContract()>
Sub Insert(Username As String, Password As String, Email As String, Cell As String, DateCreated As Date, LastLogin As Date, Lat1 As String, Lat2 As String, Lat3 As String, Lon1 As String, Lon2 As String, Lon3 As String, Loc1Date As Date, Loc2Date As Date, Loc3Date As Date, Ip1 As String, Ip2 As String, Ip3 As String, IMEI1 As String, IMEI2 As String, IMEI3 As String)
<OperationContract()>
Sub Update(UserId As Integer, Username As String, Password As String, Email As String, Cell As String, DateCreated As Date, LastLogin As Date, Lat1 As String, Lat2 As String, Lat3 As String, Lon1 As String, Lon2 As String, Lon3 As String, Loc1Date As Date, Loc2Date As Date, Loc3Date As Date, Ip1 As String, Ip2 As String, Ip3 As String, IMEI1 As String, IMEI2 As String, IMEI3 As String)
<OperationContract()>
Sub SelectUser(UserId As Integer, Username As String, Password As String, Email As String, Cell As String, DateCreated As Date, LastLogin As Date, Lat1 As String, Lat2 As String, Lat3 As String, Lon1 As String, Lon2 As String, Lon3 As String, Loc1Date As Date, Loc2Date As Date, Loc3Date As Date, Ip1 As String, Ip2 As String, Ip3 As String, IMEI1 As String, IMEI2 As String, IMEI3 As String)
<OperationContract()>
Sub UserLogin(Username As String, Password As String)
<OperationContract()>
Sub Delete(UserId As Integer)
End Interface
<DataContract()>
Public Class UserData
Public Sub New()
Me.UserTable = New DataTable("Users")
End Sub
<DataMember()>
Public Property UserTable() As DataTable
End Class
Public Function [GetUser]() As UserData Implements IService1.GetUser
Dim constr As String = ConfigurationManager.ConnectionStrings("nighstanddata").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT UserId, Username, Password, Email, Cell, DateCreated, LastLogin, Lat1, Lat2, Lat3, Lon1, Lon2, Lon3, Loc1Date, Loc2Date, Loc3Date, Ip1, Ip2, Ip3, IMEI1, IMEI2, IMEI3 FROM Users")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
Dim User As New UserData()
sda.Fill(User.UserTable)
Return User
End Using
End Using
End Using
End Using
End Function
Public Sub UserLogin(Username As String, Password As String) Implements IService1.UserLogin
Dim wrapper As New E3Des("MyMusterISubMIt")
Dim constr As String = ConfigurationManager.ConnectionStrings("nighstanddata").ConnectionString
Dim cmd As New SqlCommand()
Dim con As New SqlConnection(constr)
cmd.Connection = con
cmd.CommandText = "Select Username, Password From Users WHERE (Username=@Username AND Password=@Password)"
cmd.Parameters.AddWithValue("@Username", wrapper.EncryptData(Username))
cmd.Parameters.AddWithValue("@Password", EncryptMan(Password))
con.Open()
Dim rd As Integer = cmd.ExecuteNonQuery()
If rd <> 1 Then
ClientScript.RegisterStartupScript(Me.GetType(), "AlertMessageBox", "alert('Wrong login credintials');", True)
Else
ClientScript.RegisterStartupScript(Me.GetType(), "AlertMessageBox", "alert('Login Successful');", True)
End If
con.Close()
End Sub
End Class
Web.config file looks like this:
="1.0"
<configuration>
<connectionStrings>
<add name="nighstanddata" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=nddata;Integrated Security=True" />
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Now in my Intel XDK App (Application Framework), I have created a web service called MyApi. Two files where created, a javascript file and Json file. This is where my confusion comes. I have no Json Background but I googled and followed some online tutorials, but still I couldn't get this to work. I have used the placeholder code and made few changes several times, without luck.
I want to be able to login and also insert and update database records.
Here is my current .Json file content:
{
"endpoints": [
{
"name": "InsertUser",
"dashboardUrl": "http://localhost:57014/Service1.svc",
"methods": [
{
"MethodName": "Insert",
"Synopsis": "Insert new recors in user"
},
{
"MethodName": "Update",
"Synopsis": "Updates selected User record",
"parameters": [
{
"Name": "UserId",
"Required": "Y",
"Default": "",
"Type": "number",
"Description": "UserId to be updated."
}
]
},
{
"MethodName": "SelectUser",
"Synopsis": "Selects User record",
"parameters": [
{
"Name": "UserId",
"Required": "Y",
"Default": "",
"Type": "number",
"Description": "UserId to be selected."
}
]
},
{
"MethodName": "methodA1Authenticate",
"Synopsis": "This is a bare-boned implementation of basic OAuth 2 authentication. We suggest you hook this method up to your login button. Depending on the service, other parameters may be necessary.",
"parameters": [
{
"Name": "response_type",
"Required": "Y",
"Default": "code",
"Type": "string",
"Description": "code for authentication code, token for implicit"
},
{
"Name": "redirect_uri",
"Required": "Y",
"Default": "http://127.0.0.1:58889/Home",
"Type": "string",
"Description": "The redirect_uri must match the redirect uri in your app settings on the API website. You must use the localhost/127.0.0.1 url provided for the services tab or else your service won't work, but you are free to use whatever url you want after that."
}
]
},
{
"MethodName": "authenticatedMethodA1",
"Synopsis": "A function call that requires an access token acquired from first authenticating the service",
"parameters": [
{
"Name": "param_1_name",
"Required": "Y",
"Default": "",
"Type": "string",
"Description": "Description of the first parameter."
}
]
}
]
}
My .Js file:
(function (credentials, helpers) {
var exports = {};
exports.methodA1 = function (params) {
var url = 'http://localhost:57014/Service1.svc';
return $.ajax({url: url});
};
exports.methodA1User = function(params) {
var url = 'http://localhost:57014/Service1.svc/GetUser/' + params.UserId;
delete params.UserId;
if (params) url = url + '?' + $.param(params);
return $.ajax({url: url, type: 'GET'});
};
exports.methodA1Authenticate = function(params) {
var url = {
codeUrl: 'http://localhost:57014/Service1.svc/UserLogin?',
tokenUrl: 'http://localhost:57014/access_token?'
};
var urlParams = {
code: {
client_id: credentials.apiKey,
redirect_uri: params.redirect_uri,
response_type: params.response_type
},
token: {
client_secret: credentials.apiSecret,
grant_type: 'authorization_code'
}
};
return helpers.oauth2AuthCode(url, urlParams)
.then(function(token){
var db = window.localStorage;
db.setItem('service_access_token', token);
return token;
})
.fail(function(err){
console.log(err);
});
};
exports.authenticatedMethodA1 = function(params){
var token = window.localStorage.getItem('service_access_token')
if (!token) return 'Need access token before making call';
var urlParams = $.extend({access_token: token}, params);
var completeUrl = 'http://localhost:57014/Service1.svc/UserLogin?' + $.param(urlParams);
return $.ajax({
url: completeUrl,
type: 'GET',
dataType: 'json'})
.then(function(response){
return response;
})
.fail(function(err){
return err.responseText;
});
};
return exports;
})
What I have tried:
I have used the placeholder code and made few changes several times, without luck.