-
-
Save cvbarros/04db931a2c257026aef1 to your computer and use it in GitHub Desktop.
Ninject and NHibernate - Setting user credentials in the database
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
using System; | |
using NHibernate.Cfg; | |
using Ninject; | |
using Ninject.Activation; | |
using Ninject.Modules; | |
namespace NinjectAuthenticationThenBind | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var kernel = new StandardKernel(new DatabaseModule()); | |
var authForm = kernel.Get<AuthenticationForm>(); | |
authForm.RequestCredentials(); | |
var someOtherForm = kernel.Get<SomeOtherForm>(); | |
someOtherForm.Display(); | |
Console.ReadLine(); | |
} | |
} | |
public class AuthenticationForm | |
{ | |
private readonly IAuthenticationService _authService; | |
private readonly IConnectionStringProvider _connectionStringProvider; | |
public AuthenticationForm(IAuthenticationService authService, IConnectionStringProvider connectionStringProvider) | |
{ | |
_authService = authService; | |
_connectionStringProvider = connectionStringProvider; | |
} | |
public void RequestCredentials() | |
{ | |
Console.Write("DB Instance: "); | |
var dbInstance = Console.ReadLine(); | |
Console.Write("Username: "); | |
var username = Console.ReadLine(); | |
Console.Write("Password: "); | |
var password = Console.ReadLine(); | |
Console.WriteLine("Before sign in: {0}", _connectionStringProvider.GetConnectionString()); | |
_authService.SignIn(dbInstance, username, password); | |
} | |
} | |
public class SomeOtherForm | |
{ | |
private readonly Configuration _dependencyExample; | |
public SomeOtherForm(Configuration dependencyExample) | |
{ | |
_dependencyExample = dependencyExample; | |
} | |
public void Display() | |
{ | |
Console.WriteLine(_dependencyExample.Properties["connection.connection_string"]); | |
} | |
} | |
public class DatabaseModule : NinjectModule | |
{ | |
public override void Load() | |
{ | |
Bind<Configuration>().ToProvider<ConfigurationProvider>(); | |
Bind<IAuthenticationService>().To<AuthenticationService>(); | |
Bind<IConnectionStringProvider>().To<ConnectionStringProvider>(); | |
//Use Current user if available. If not, use default credentials. | |
Bind<IDatabaseUserCredentials>().ToMethod(c => DatabaseUserCredentials.Current ?? DatabaseUserCredentials.Default); | |
} | |
} | |
public interface IAuthenticationService | |
{ | |
void SignIn(string dbInstance, string login, string password); | |
} | |
public class ConfigurationProvider : Provider<Configuration> | |
{ | |
private readonly IConnectionStringProvider _connectionStringProvider; | |
public ConfigurationProvider(IConnectionStringProvider connectionStringProvider) | |
{ | |
_connectionStringProvider = connectionStringProvider; | |
} | |
protected override Configuration CreateInstance(IContext context) | |
{ | |
var c = new Configuration(); | |
c.Properties["connection.connection_string"] = _connectionStringProvider.GetConnectionString(); | |
return c; | |
} | |
} | |
public interface IConnectionStringProvider | |
{ | |
string GetConnectionString(); | |
} | |
public class ConnectionStringProvider : IConnectionStringProvider | |
{ | |
private readonly IDatabaseUserCredentials _user; | |
private const string DefaultConnectionString = "database={0};user={1};password={2}"; | |
public ConnectionStringProvider(IDatabaseUserCredentials user) | |
{ | |
_user = user; | |
} | |
public string GetConnectionString() | |
{ | |
return string.Format(DefaultConnectionString, _user.DatabaseInstanceName, _user.Login, _user.Password); | |
} | |
} | |
public interface IDatabaseUserCredentials | |
{ | |
string DatabaseInstanceName { get; set; } | |
string Login { get; set; } | |
string Password { get; set; } | |
} | |
public class DatabaseUserCredentials : IDatabaseUserCredentials | |
{ | |
internal static DatabaseUserCredentials Current = null; | |
public static DatabaseUserCredentials Default = new DatabaseUserCredentials("securityDb", "securityLogin", "securityPassword"); | |
public string DatabaseInstanceName { get; set; } | |
public string Login { get; set; } | |
public string Password { get; set; } | |
internal DatabaseUserCredentials(string databaseInstanceName, string login, string password) | |
{ | |
DatabaseInstanceName = databaseInstanceName; | |
Login = login; | |
Password = password; | |
} | |
} | |
public class AuthenticationService : IAuthenticationService | |
{ | |
public void SignIn(string dbInstance, string login, string password) | |
{ | |
var user = new DatabaseUserCredentials(dbInstance, login, password); | |
// A single point to manipulate your singleton instance | |
DatabaseUserCredentials.Current = user; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment