In Xamarin UWP

AmalaKalpanaAmalaKalpana ✭✭Member ✭✭

I have Username and Password and I want to remember the username and password. How can I do this?

Answers

  • AlessandroCaliaroAlessandroCaliaro ✭✭✭✭✭ ITMember ✭✭✭✭✭
    Save it somewhere
  • AmalaKalpanaAmalaKalpana ✭✭ Member ✭✭

    Yeah!! I know that......I need Functionality for remember user name and Password in Xamarin.

  • LandLuLandLu Xamurai Member, Xamarin Team Xamurai

    There're several ways to persist data in Xamarin Forms.
    Using Properties:

    // Store
    Application.Current.Properties["userName"] = "user name";
    Application.Current.Properties["password"] = "do some encryption here";
    Application.Current.SavePropertiesAsync();
    // Retrieve
    if (Application.Current.Properties.ContainsKey("userName"))
    {
        var userName = Application.Current.Properties["userName"];
    }
    if (Application.Current.Properties.ContainsKey("password"))
    {
        var psw = Application.Current.Properties["password"];
    }
    

    Using Xamarin.Essentials: Preferences https://docs.microsoft.com/en-us/xamarin/essentials/preferences?tabs=uwp

    // Store
    Xamarin.Essentials.Preferences.Set("userName", "user name");
    // Retrieve
    var userName = Xamarin.Essentials.Preferences.Get("userName", "defaultValue");
    

    Another way is to write your information to the local folder:

    // Write
    var userInformation = new UserInfo { UserName = "name", Password = "..." };
    var jsonStr = JsonConvert.SerializeObject(userInformation);
    
    var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    var filePath = Path.Combine(folderPath, "user.txt");
    File.WriteAllText(filePath, jsonStr);
    
    // Read
    var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    var filePath = Path.Combine(folderPath, "user.txt");
    if (File.Exists(filePath))
    {
        var jsonStr = File.ReadAllText(filePath);
    
        var userModel = JsonConvert.DeserializeObject<UserInfo>(jsonStr);
    }
    
  • AmalaKalpanaAmalaKalpana ✭✭ Member ✭✭

    Thanks LandLu........ I will Check

Sign In or Register to comment.