I’m developing a WPF application using PRISM and I need to implement a Login functionality that loads before the Shell.
What is the best way to do it? Treat the login as a module? Put it in the main project altogether with the Shell?
If the login is correct, I need to store some user information (name, role, etc.) to use it later across the application (in the Shell and also in modules). Where and how should I store this information?
Thanks in advance.
This discussion describes a solution to showing the login form before the shell: http://compositewpf.codeplex.com/discussions/29570
As to how to store the user's details, you could utilise the .Net GenericPrincipal and GenericIdentity classes. They let you represent "basic" information about a user, such as their username and roles. The principal/identity can then be stored against the current thread, so you don't need to worry about creating some kind of static/singleton class through which to access the details. It also becomes easy to check the current user's role membership.
Create the objects and assign to the current thread like this:-
string[] roles = { "User", "Admin" };
Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity("Joe"),
roles);
Then whenever you want to access details of the currently logged-in user:-
public string GetUsername()
{
var principal = Thread.CurrentPrincipal;
var identity = principal == null ? null : principal.Identity;
return identity == null ? null : identity.Name;
}
public bool IsInRole(string role)
{
var principal = Thread.CurrentPrincipal;
return principal == null ? false : principal.IsInRole(role);
}
If GenericPrincipal and GenericIdentity aren't suitable, I would create a singleton class with the necessary properties (name, roles, etc) that can be passed around to other parts of the application, e.g. using an IoC container. A static class is an even easier solution (no need to pass around), but statics can make unit testing more tricky.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With