Jea*_*ers 1 .net c# authentication maui
我希望用户能够自动登录我的 .NET MAUI 应用程序。
我已经完成了登录系统,但我不知道如何在关闭应用程序后使登录保持不变。
这是到目前为止我的登录系统。
public partial class Login
{
// User credentials
string username;
string password;
// Try verification
public async Task LoginVerification()
{
// Check if credentials are valid in the database.
bool isValid = await CheckIfValid(username, password);
if (isValid)
{
Debug.WriteLine("User Was Found");
// Do stuff after succesful login.
}
else
{
Debug.WriteLine("User Not Found");
// Do stuff after unsuccesful login.
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能让他们下次启动应用程序时不必输入凭据?
小智 6
这通常是通过保存用户的登录数据,然后在必要时执行自动登录来实现的。这也确保了凭据仍然有效。
我建议将此类数据保存到.NET MAUIPreferences系统。
这适用于保存到应用程序数据的键值对。
这是将用户密码保存到用户首选项的示例。
// Create the password string
string myPass = "somePass";
// Save the password to the Preferences system
Preferences.Set("UserPassword", myPass); // The first parameter is the key
Run Code Online (Sandbox Code Playgroud)
将用户数据保存到应用程序的首选项后,您可以在应用程序启动时检索它并使用该Get()方法执行自动登录。
// Get the password from the Preferences system
string passwordFromPrefs = Preferences.Get("UserPassword", "defaultPass");
Run Code Online (Sandbox Code Playgroud)
请注意,该Get()方法要求您传递第二个参数作为默认值。如果应用程序的首选项中没有指定键的数据,该方法将返回此默认值。
| 归档时间: |
|
| 查看次数: |
1494 次 |
| 最近记录: |