Created
September 7, 2014 09:14
-
-
Save Layoric/ea3782b9c07d6e513577 to your computer and use it in GitHub Desktop.
SS Basic authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class UnitTests | |
{ | |
private readonly ServiceStackHost appHost; | |
public UnitTests() | |
{ | |
appHost = new TestAppHost(); | |
appHost.Init().Start("http://*:10100/"); | |
} | |
[TestFixtureTearDown] | |
public void TestFixtureTearDown() | |
{ | |
appHost.Dispose(); | |
} | |
[Test] | |
public void TestMethod1() | |
{ | |
var service = appHost.Container.Resolve<MyServices>(); | |
var response = (HelloResponse)service.Any(new Hello { Name = "World" }); | |
Assert.That(response.Result, Is.EqualTo("Hello, World!")); | |
} | |
[Test] | |
public void TestMethod2() | |
{ | |
const string userName = "TestUser"; | |
const string password = "TestTest123"; | |
var client = new JsonServiceClient("http://localhost:10100/"); | |
var registerResponse = client.Post<RegisterResponse>(new Register | |
{ | |
DisplayName = userName, | |
Email = "joe@test.com", | |
FirstName = "Joe", | |
LastName = "Blogs", | |
UserName = "TestUser", | |
Password = password, | |
AutoLogin = false | |
}); | |
var loginResponse = client.Post<AuthenticateResponse>(new Authenticate | |
{ | |
provider = "basic", | |
UserName = userName, | |
Password = password, | |
RememberMe = false | |
}); | |
Assert.That(loginResponse, Is.Not.Null); | |
Assert.That(loginResponse.ResponseStatus.ErrorCode,Is.Null); | |
} | |
} | |
public class TestAppHost : AppSelfHostBase | |
{ | |
public TestAppHost() | |
: base("TestAppHost", typeof(MyServices).Assembly) | |
{ | |
} | |
/// <summary> | |
/// Application specific configuration | |
/// This method should initialize any IoC resources utilized by your web service classes. | |
/// </summary> | |
/// <param name="container"></param> | |
public override void Configure(Container container) | |
{ | |
//Config examples | |
this.Plugins.Add(new PostmanFeature()); | |
//this.AddPlugin(new CorsFeature()); | |
//Construct an array of auth providers that will allow users to authenticate with your application | |
var authProviders = new List<IAuthProvider>(); | |
//In this example, just the CredentialsAuthProvider | |
authProviders.Add(new BasicAuthProvider()); | |
//Create the AuthFeature plugin, registering the factory method that will create the session. See SessionFactory method below. | |
var authFeature = new AuthFeature(SessionFactory, authProviders.ToArray()); | |
//Add the feature to the AppHost | |
this.Plugins.Add(authFeature); | |
//Register a connection factory for your database, in this case Sqlite, this is our persistence layer used by our user repository | |
container.Register<IDbConnectionFactory>(new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider)); | |
//Register a user repository, specifying OrmLite with our new connection factory. | |
container.Register<IUserAuthRepository>(new OrmLiteAuthRepository(container.Resolve<IDbConnectionFactory>())); | |
//Initialize the user repository schema | |
container.Resolve<IUserAuthRepository>().InitSchema(); | |
//Register a cachle client to be used for session management. | |
container.Register<ICacheClient>(new MemoryCacheClient()); | |
//Enable user registration using the RegistrationFeature plugin. | |
this.Plugins.Add(new RegistrationFeature()); | |
} | |
/// <summary> | |
/// A method that generates a new AuthUserSession when a session trying to authenticate. | |
/// </summary> | |
/// <returns></returns> | |
private IAuthSession SessionFactory() | |
{ | |
return new AuthUserSession(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment