How can I persist a login?

I am writing an application that does not necessarily require a user to login (some features available to unauthenticated users).

I want to be able to persist a couple of custom objects, like the 'User', some 'Roles', and whether or not they are authenticated.

When a user starts the app, the get some screens, but must authenticate to get others.

What would be the best way to persist that info.

I am an ASP.NET guru ;-), but not a Windows Forms genius.

Thanks.




Answer this question

How can I persist a login?

  • tbohon

    Hi Smith,

    Is this a windows forms application If yes then you can set access privileges set for each screen in the backend. When a user clicks on a screen, in the form load, you can call a global function which checks the user access permissions for that particular screen. Hope this helps.

    Thank you,
    Bhanu.



  • Justin5

    I am not sure of it because i am unable to clearly visualize your app. You can give it a try and see if that works fine.

    thank you,
    Bhanu.



  • Lauriew

    Yes, forms app.

    I guess my question was how to persist the user object itself.

    Your suggestion is great, but that requires a user to compare against any access privileges.

    Can I create a static object in the Program class to treat as a singleton global of sorts.

    My 'User' object is a complex type that when someone logs in, I would like to persist that across the application for exactly the reasons you described.

    Any downside to just creating a static User object in the Program class

    Thanks.



  • jinath

    No, it would not be inaccessible, it is just that if you have multiple threads which try to write to that object, you should synchronize the access. And no, in a standard application you have only one thread if you do not explicitly start new ones.

    Although, I would embed the static User directly into the User class itself and even, if the User object should be really unique, make the constructor private, so that you can access your one and only user from everywhere by User.Current:

    public class User
    {
    private string userName;
    private string firstName;
    private string lastName;
    private bool isAuthenticated;
    // ...
    private static User current;

    static
    User()
    {
    current =
    new User();
    }

    private User()
    {
    }

    public
    static User Current
    {
    get { return current; }
    }

    public string UserName
    {
    get { return this.userName; }
    set { this.userName = value; }
    }
    }


  • andplo

    A static class or a singleton pattern is fine for the information you want to store. Only if you change the information during runtime and if you have multiple threads possibly accessing the static class or the single object instance, you should use "lock" to synchronize access to it.
  • Origamime

    In the Program class, I have a: public static User CurrentUser;

    Where 'User' is a complex type (class) like:

    public class User

    {
    string lastName;
    string firstName;
    bool isAuthenticated;
    // and so on and so forth
    }

    When the user logs in, it just sets those values.

    As far as multi-threading goes, would I ever have more than one thread of the main application

    If I started ANY new thread, would this be inaccessible

    Thanks again.



  • How can I persist a login?