简体   繁体   English

如何保持用户在Windows Phone应用中的登录状态?

[英]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. 我正在使用C#开发Windows 10通用应用程序,并且使用PHP和MySQL验证,存储数据并向用户显示数据。

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. 因此,在登录过程中,我将用户电子邮件和密码发送到PHP文件,并在那里将数据与存储在数据库中的数据进行比较,然后将答案(成功或失败)发送给应用程序。

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 ? 现在我有一个问题,我想知道在关闭应用程序并重新打开应用程序后,或者即使重新启动手机或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 ... ? 我可以直接在C#中执行此操作(Microsoft是否提供任何API),或者我需要3d方库或...?

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. 这些类似于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: 在这里您可以阅读有关如何使用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 https://msdn.microsoft.com/zh-CN/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. 您可以使用ApplicationData.Current.LocalSettings来存储和获取值。

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;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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