如何在LoginFlow中添加多个用户-Xamarin系统 [英] How to add multiple users in LoginFlow - Xamarin system

查看:112
本文介绍了如何在LoginFlow中添加多个用户-Xamarin系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有样品xamarin形式的解决方案.我想要用户名和登录功能.我正在做一些研究,发现了一个很好的例子.

I have sample xamarin forms solution. I wanted username and login functionality. I was doing some research and found nice example.

https://developer.xamarin.com/samples/xamarin-表格/导航/LoginFlow/

我喜欢"LoginFlow"的工作方式.

I like the way 'LoginFlow' works.

我想将用户帐户硬编码为解决方案,而登录时,用户将仅使用其凭据,而我将对其进行硬编码并将其单独分发出去.

I want to hardcode the user accounts into solution and than when it comes to login user would just use their credentials which I will hard code and give them out individually.

我可以看到有一个名为"LoginNavigation-> Constants.cs"的文件.它有一个用户帐户.

I can see there is file called "LoginNavigation->Constants.cs". It has one user account.

我的问题是我如何在其中添加多个用户,以便多个用户可以登录此解决方案.

My Question is how I can add multiple users in this so multiple user can login into this solution.

namespace LoginNavigation
 {
   public static class Constants
     {
        public static string Username = "Xamarin";
        public static string Password = "password";


       }
  }

修改

将带有登录表单的凭据与目录匹配时出错.

Getting error when matching credentials with login form to directory.

Constants.cs

using System;
using System.Collections.Generic;

namespace LoginNavigation
{
    public class Constants
    {
        Dictionary<string, string> Credentials = new Dictionary<string, string>()
    {
        { "user1", "pass" },
        { "user2", "pass" },
        { "user3", "pass" }
    };

    }


}

在登录页面上;这是用户单击登录后的动作.

On the login page; This is action when user click login.

LoginPage.cs

        async void OnLoginButtonClicked (object sender, EventArgs e)
         {
            var user = new User {
            Username = usernameEntry.Text,
            Password = passwordEntry.Text
        };

        var isValid = AreCredentialsCorrect (user);
        if (isValid) {
            App.IsUserLoggedIn = true;
            //Navigation.InsertPageBefore (new MainPageCS (), this);
            Navigation.InsertPageBefore(new MainPageCS(), Navigation.NavigationStack.First());
            await Navigation.PopAsync ();
        } else {
            messageLabel.Text = "Login failed";
            passwordEntry.Text = string.Empty;
        }
    }


    bool AreCredentialsCorrect (string user, string pass)
    {
        //return user.Username == Constants.Username && user.Password == Constants.Password;
        return (Credentials.ContainsKey(user) && Credentials[user] == pass);

    }

请问如何解决以上问题.谢谢

How I can fix above please. Thank you

推荐答案

创建一个包含所有用户/密码对的字典

create a dictionary with all of your user/password pairs in it

Dictionary<string, string> Credentials = new Dictionary<string, string>()
    {
        { "user1", "pass" },
        { "user2", "pass" },
        { "user3", "pass" }
    };

然后在您要检查它们时

public bool ValidateLogin(string user, string pass) 
{
  return (Credentials.ContainsKey(user) && Credentials[user] == pass);
}

这篇关于如何在LoginFlow中添加多个用户-Xamarin系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆