Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceStack authentication request fails
    text
    copied!<p>I am trying to set up authentication with my ServiceStack service by following <a href="http://enehana.nohea.com/general/customizing-iauthprovider-for-servicestack-net-step-by-step/" rel="nofollow">this tutorial</a>.</p> <p>My service is decorated with the <code>[Authenticate]</code> attribute.</p> <p>My AppHost looks like this:</p> <pre><code>public class TestAppHost : AppHostHttpListenerBase { public TestAppHost() : base("TestService", typeof(TestService).Assembly) { } public static void ConfigureAppHost(IAppHost host, Container container) { try { // Set JSON web services to return idiomatic JSON camelCase properties. ServiceStack.Text.JsConfig.EmitCamelCaseNames = true; // Configure the IOC container IoC.Configure(container); // Configure ServiceStack authentication to use our custom authentication providers. var appSettings = new AppSettings(); host.Plugins.Add(new AuthFeature(() =&gt; new AuthUserSession(), // use ServiceStack's session class but fill it with our own data using our own auth service provider new IAuthProvider[] { new UserCredentialsAuthProvider(appSettings) })); } } </code></pre> <p>where <code>UserCredentialsAuthProvider</code> is my custom credentials provider:</p> <pre><code>public class UserCredentialsAuthProvider : CredentialsAuthProvider { public override bool TryAuthenticate(IServiceBase authService, string userName, string password) { try { // Authenticate the user. var userRepo = authService.TryResolve&lt;IUserRepository&gt;(); var user = userRepo.Authenticate(userName, password); // Populate session properties. var session = authService.GetSession(); session.IsAuthenticated = true; session.CreatedAt = DateTime.UtcNow; session.DisplayName = user.FullName; session.UserAuthName = session.UserName = user.Username; session.UserAuthId = user.ID.ToString(); } catch (Exception ex) { // ... Log exception ... return false; } return true; } } </code></pre> <p>In my user tests I initialize and start my TestAppHost on <code>http://127.0.0.1:8888</code>, then use JsonServiceClient to authenticate itself to the service like so:</p> <pre><code>var client = new JsonServiceClient("http://127.0.0.1:8888/") var response = client.Send&lt;AuthResponse&gt;(new Auth { provider = UserCredentialsAuthProvider.Name, UserName = username, Password = password, RememberMe = true }); </code></pre> <p>But getting the following exception:</p> <pre><code>The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetResponse() at ServiceStack.ServiceClient.Web.ServiceClientBase.Send[TResponse](Object request)... </code></pre> <p>The <code>ServiceStack.ServiceInterface.Auth.Auth</code> request contains the correct username and passsword, and the request is being posted to: </p> <pre><code>http://127.0.0.1:8888/json/syncreply/Auth </code></pre> <p>I am not sure why the URL is not <code>/json/auth/credentials</code> or what I might be doing wrong. Any suggestions?</p> <p><hr> <strong>UPDATE</strong></p> <p>Tracing the chain of events up the stack I found the following:</p> <p><code>JsonDataContractSerializer.SerializeToStream</code> correctly serializes the Auth request into Json. However, the <code>System.Net.HttpRequestStream</code> passed to <code>JsonDataContractDeserializer</code> by <code>EndpointHandlerBase</code> has a stream of the correct length that is filled with nulls (zero bytes). As a result, the request object passed to <code>CredentialsAuthProvider.Authenticate</code> has nulls in all its properties.</p> <p>How can the HTTP stream get stripped of its data?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload