Question

I was reading this from their documentation which says:

Then you need to register your custom credentials auth provider:

  //Register all Authentication methods you want to enable for this web app.
  Plugins.Add(new AuthFeature(() => new AuthUserSession(),
      new IAuthProvider[] {
    new CustomCredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
      }
  ));

My question is: Where do I put this? Also, what is meant by the comment, "HTML Form post of UserName/Password credentials?"

Currently, I have a ServiceStack service which returns JSON when called. I want to add an Authorize attribute above it so that only authorized users will be able to access it.

I have created a class as they suggest:

public class CustomCredentialsAuthProvider : CredentialsAuthProvider
{
    public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
    {
        //Add here your custom auth logic (database calls etc)
        //Return true if credentials are valid, otherwise false
        return false;
    }

    public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        //Fill the IAuthSession with data which you want to retrieve in the app eg:
        session.FirstName = "some_firstname_from_db";
        //...

        //Important: You need to save the session!
        authService.SaveSession(session, SessionExpiry);
    }
}

How can I "register your custom credentials auth provider?"

Was it helpful?

Solution

You register your plugins within the Configure method of your AppHost. The comment was just used in the example that you pulled the code from to suggest that the CustomCredentialsAuthProvider would work with a HTTP POST from a form.

public class MyApphost : AppHostHttpListenerBase
{
    public MyApphost() : base("Service Name", typeof(MyApphost).Assembly) {}

    public override void Configure(Container container)
    {
        Plugins.Add(new AuthFeature(
            () => new AuthUserSession(),
            new IAuthProvider[] { new CustomCredentialsAuthProvider()}
        ));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top