Basic authentication using the service stack

I am using JsonServiceClient in my Android application (written with Xamerin). I have a test client that works with the HelloWorld example listed on the servicestack website. It works fine without authentication and quickly returns values.

Now I am trying to bring authentication to the mix, starting with a very simple authentication. I have my own auth and session class on the server that looks like this:

    public class userSession : AuthUserSession
    {
        public string clientCode { get; set; }
    }

    public class userAuth : CredentialsAuthProvider
    {
        public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
        {
            if (userName == "user" || password == "1234") {
                var session = (userSession)authService.GetSession(false);
                session.clientCode = "peruse"; 
                return true ; 
            } else {
                return false;
            }
        }
    }

and it is set to:

        // auth feature and session feature
        Plugins.Add(new AuthFeature(
            () => new userSession(),
            new[] { new userAuth() }
        ) { HtmlRedirect = null } );

On the client side, I call the new JsonServerClient with:

JsonServiceClient client = new ServiceStack.ServiceClient.Web.JsonServiceClient("http://172.16.0.15/");

And the event for the button on the Android interface:

            try 
            {
                client.SetCredentials("user", "1234"); 
                HelloResponse response = client.Get<HelloResponse>("/hello/" + toSum.Text);
                txtResult.Text = response.Result ; 
            }
            catch (Exception ex) 
            {
                txtResult.Text = ex.Message; 
            }

I keep getting 404 back from the server. When I try to access it cURL from Linux:

curl -v http://user:1234@172.16.0.15/hello/5

It returns:

*   Trying 172.16.0.15... connected
* Server auth using Basic with user 'user'
> GET /hello/5 HTTP/1.1
> Authorization: Basic dXNlcjoxMjM0

(Other verbose things ... then ...)

 HTTP/1.1 302 Found

, :

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/login.aspx?ReturnUrl=%2fhello%2f5">here</a></h2>
</body><html>

Web.config , .

, : ? , , , ?

+4
1

, , : - ServiceStack SetCredentials , :

class Program
{
    const string BaseUrl = "http://localhost:8088/api";

    static void Main(string[] args)
    {
        var restClient = new JsonServiceClient(BaseUrl);

        restClient.SetCredentials("john", "test");

        restClient.AlwaysSendBasicAuthHeader = true;

        HelloResponse response = restClient.Get<HelloResponse>("/hello/Leniel");

        Console.WriteLine(response.Result);
    }
}

//Response DTO
//Follows naming convention
public class HelloResponse
{
    public string Result { get; set; }
}

, .

+1

All Articles