You are not logged in.
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
Hmm, maybe that will help. I try, thanks.
Offline
Sorry, it didn't change anything. still got same error:
Message "The remote server returned an error: (403) Forbidden."
Status ProtocolError
Offline
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
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
I am using TSQLRestServerAuthenticationHttpBasic now, but still getting same error before.
Offline
@koraycayiroglu - can you set a breakpoints into TSQLRestServerAuthenticationHttpBasic.Auth / TSQLRestServerAuthenticationHttpBasic.RetrieveSession and look what is wrong?
Offline
@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
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
if we can work this out, this source will be a good example for mormot users.
Offline
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
you guys are really helpful
Offline
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.
Online
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
Ok, my fault. i found some documantation
Offline
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
Hi,
Here is an example of a c# client connecting to a mormot server:
https://github.com/velissariouc/mormotCSharpClient
Offline
Very nice sample indeed!
Thank you, velissariouc
Offline