简体   繁体   中英

How keep user logged-in in a windows phone app?

I'm working on a windows 10 universal app using C# and I use PHP and MySQL to validate, store and show the data to the user.

So while login process, I send user email and password to the PHP file and there I compare data against the data stored into the database and send an answer (success or failed) to the app.

Now I have a question, I would like to know how I can keep users logged in after they close the app and reopen it or even if they restart their phone or their PC ?

I searched about this subject and as I understood, I need to store some data on the user machine and check that data when user reopen the app, but I didn't understand how I can do that or where to store the data ! Can I do that directly in C# (does Microsoft any API) or I need a 3d party library or ... ?

Your assumption is correct. You have to store locally the info about whether or not the user is logged in. To do that you can use the Local Settings property. Those are similar to the shared-preferences in android studio. The main idea is to store a key-value pair locally. In your case your key would be some string that you will use to restore the data and your value could be a boolean variable that stores whether or not the user is logged in or not. Every time when you open the app you will first restore the boolean and check if the user is logged in and so on... This is only a suggestion of how to use this concept but since you are not giving any code I can't extend it and give you a proper example using your own code. Here you can read on how to use LocalSettings:

https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.storage.applicationdata.localsettings?cs-save-lang=1&cs-lang=csharp#code-snippet-2

You can use ApplicationData.Current.LocalSettings to store and get values.

I prefer serializing the objects and store the serialized strings because ín that manner you can also store complex types. But you could also skip the part where i serialize and just add your bool here.

Below is some generic code:

    public void Set<T>(string key, T value)
    {
        string serialized = JsonConvert.SerializeObject(value);
        var settings = ApplicationData.Current.LocalSettings.Values;
        if (!settings.Keys.Contains(key))
        {
            settings.Add(key, serialized);
        }
        else
        {
            settings[key] = serialized;
        }
    }

    private bool TryGet<T>(string key, out T result)
    {
        var settings = ApplicationData.Current.LocalSettings.Values;
        if (!settings.Keys.Contains(key))
        {
            result = default(T);
            return false;
        }
        var value = settings[key] as string;

        try
        {
            result = JsonConvert.DeserializeObject(value, typeof(T));
            return true;
        }
        catch
        {
            result = default(T);
            return false;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM