#1 2018-12-04 14:18:11

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Need help, how can i connect to mormot server via .net webapp with c#?

Hello,

I am using mormot within my delphi applications for a while and it's really good. thank you for your hard work. I have a windows service application as a rest/http server for my application using default authentication. here is the code how i implement rest and http server:

      fRestServer := TSQLRestServerDB.Create(Model, DBFileName, true);
      fRestServer.CreateMissingTables;
      fRestServer.AuthenticationRegister(TSQLRestServerAuthenticationDefault);

      fHTTPServer := TSQLHttpServer.Create(AnsiString(IntToStr(PortNo)), [fRestServer], '+', useHttpApi);
      THttpServer(fHTTPServer.HttpServer).ServerKeepAliveTimeOut := CONNECTION_TIMEOUT;

with delphi, i don't have any problems but i need to connect this service via http with c#:

I tried to change authentication type to httpbasic and put a header for my http request but it respons as "Request denied (403)". Waht should i do to make it work.

c# code:

            String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(UserName + ":" + Password));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:888/api/listusers");
            request.Method = "GET";
            request.Headers.Add("Authorization", "Basic " + encoded);
            //request.PreAuthenticate = true;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();

            string html = string.Empty;

            using (StreamReader reader = new StreamReader(stream))
            {
                html = reader.ReadToEnd();
            }

Help will be really appreciated but thanks to everyone anyway.

Best regards

Offline

#2 2018-12-04 14:49:19

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Need help, how can i connect to mormot server via .net webapp with c#?

mORMot implementation use UTF-8 encoding for user:password pair (instead of "ISO-8859-1")

Offline

#3 2018-12-05 07:43:18

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Hmm, maybe that will help. I try, thanks.

Offline

#4 2018-12-05 07:57:20

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Sorry, it didn't change anything. still got same error:

Message	"The remote server returned an error: (403) Forbidden."
Status	ProtocolError

Offline

#5 2018-12-05 10:17:38

Chaa
Member
Registered: 2011-03-26
Posts: 249

Re: Need help, how can i connect to mormot server via .net webapp with c#?

TSQLRestServerAuthenticationDefault class implements a proprietary (mORMot) RESTful Authentication with URI signing.
In your example you must use TSQLRestServerAuthenticationHttpBasic for AuthenticationRegister.

Last edited by Chaa (2018-12-05 10:19:45)

Offline

#6 2018-12-05 13:04:53

cybexr
Member
Registered: 2016-09-14
Posts: 81

Re: Need help, how can i connect to mormot server via .net webapp with c#?

As @Chaa stated , your server code users Restful auth, but C# send HTTP Basic auth, so you get 403

see docs https://synopse.info/files/html/Synopse … #TITLE_535

Offline

#7 2018-12-06 09:16:12

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

I am using TSQLRestServerAuthenticationHttpBasic now, but still getting same error before.

Offline

#8 2018-12-06 13:04:07

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Need help, how can i connect to mormot server via .net webapp with c#?

@koraycayiroglu - can you set a breakpoints into TSQLRestServerAuthenticationHttpBasic.Auth / TSQLRestServerAuthenticationHttpBasic.RetrieveSession and look what is wrong?

Offline

#9 2018-12-06 13:45:05

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

@mvp, thanks i put breakpoints to both of them and found something strange.

It stops at TSQLRestServerAuthenticationHttpBasic.RetrieveSession, but never enters TSQLRestServerAuthenticationHttpBasic.Auth. After retrievesession resulted as nil, c# get respond as Denied (403).

Best regards

Offline

#10 2018-12-07 09:11:36

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

I made a sample project. Both for delphi/mormotserver and c#/webapp. I hope someone can help me.


https://www.dropbox.com/s/2nmeg7y7rugy6 … t.rar?dl=0

Last edited by koraycayiroglu (2018-12-07 09:11:55)

Offline

#11 2018-12-07 11:00:37

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

if we can work this out, this source will be a good example for mormot users.

Offline

#12 2018-12-11 10:07:51

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Hello again,

I was reading documentation and looking for a solution to my problem. Basically i think i need to create a session before authentication, but i though basic authentication does not need session management. So, i tried to post my credentials and get a session signature for client to use.

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:888/api/auth");
            request.Method = "POST";
            request.Credentials = new NetworkCredential(username, password);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AuthenticationResponse));
            AuthenticationResponse userObject = (AuthenticationResponse)serializer.ReadObject(stream);

            request = (HttpWebRequest)WebRequest.Create(requesturl + @"&session_signature=" + userObject.result.ToString());
            request.Method = "GET";

But result is not like signatures as shown in documentation. I am not a c# expert, only use api with in mobile apps and they have very easy to setup, and never need to create session for basic authentication. By the way if is there a better way to connect mormot api server i can use it, i am not going after basic because of some requirement. i chose it because i thought it will be easy enough for simple test application.

So, how can i create a session within mormot and get session_signature, or how can i bypass session management for basic authentication?

Best regards

Offline

#13 2018-12-17 08:26:10

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

you guys are really helpful

Offline

#14 2018-12-17 11:41:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: Need help, how can i connect to mormot server via .net webapp with c#?

For C#, Java or JavaScript clients, we usually don't use mORMot sessions, nor the mORMot authentication.
We rather define a JWT using TSQLRestServer.JWTForUnauthenticatedRequest, and a stateless - i.e. session-less - dedicated API.

The JWT token is retrieved using a dedicated service, to validate the name/password credentials.

mORMot clients would be able to use regular mORMot authentication and sessions if needed, whereas the non Delphi/FPC clients will use a regular/standard API with a JWT.

Offline

#15 2018-12-19 12:45:29

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Thank you ab for responding to my post. Is there any example or documantation i can look at? Because i couldn't find anything about JWTForUnauthenticatedRequest in main document.

best regards

Offline

#16 2018-12-20 08:00:15

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Ok, my fault. i found some documantation

Offline

#17 2018-12-20 13:01:40

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Hi,

I read lots of documents, check lots of c# examples and how to use JWT authentication but all i saw was tokens, payloads and such. I am a delphi developer, nor a JS,C#,WEB expert so i am really struggling. some my questions maybe sound stupid, sorry about that before i begin.


I am using Interface-Based api structure with multiple interfaces registered to my restserver. A typical delphi client, use RestClient, authenticate with username/passsword and then use these interfaces to execute api calls to server.

What i understand from JWT, it uses a secret key to authenticate, and get a token to use but how do i authenticate bu using username and password as i do within Delphi App and get my TSQLAuthUser and credentials to my holding?

Also, how should i call my interface based api servive with this token? There isn't any kind of example about this. I looked at forums, read documentations and ab's pdf documents and nope, nowhere to find a good example of usage.

When i call an interfaced method from delphi client, i think it use HTTP GET call to work and url looks like  localhost:888/root/interfacename/calculate?a=1&b=2. ı can make a webrequest from c# too but how should i send token to server for instance validation?

Best regrads

Offline

#18 2018-12-30 16:47:12

velissariouc
Member
Registered: 2016-10-31
Posts: 6

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Hi,
Here is an example of a c# client connecting to a mormot server:
https://github.com/velissariouc/mormotCSharpClient

Offline

#19 2018-12-30 20:04:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,686
Website

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Very nice sample!

Offline

#20 2019-01-08 07:28:55

koraycayiroglu
Member
Registered: 2017-02-03
Posts: 55

Re: Need help, how can i connect to mormot server via .net webapp with c#?

Very nice sample indeed!

Thank you, velissariouc

Offline

Board footer

Powered by FluxBB