Xamarin的.表单:如何验证登录的用户

本文关键字:验证 用户 登录 何验证 表单 Xamarin | 更新日期: 2023-09-27 18:02:50

大家好。我正在创建一个简单的Xamarin。便携式形式。到目前为止,我正在创建一个Login函数,它应该允许所有注册用户登录到我的系统。

我能够使用WEB API检索所有用户信息(用户名和密码)。

我想做的是,每当用户在我的用户名和密码文本框中输入数据时,系统应该得到它的值,并检查由我的WEB API收集的记录上是否存在关键数据。

如果存在,则用户登录系统,否则不登录。

希望你能理解我的处境。非常感谢。

下面是我的一些代码:

LoginController.cs in WebForms

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebFormsDemo;
using WebFormsDemo.ViewModel;
namespace WebFormsDemo.Controllers
{
    public class LoginController : ApiController
    {
        private EBMSEntities db = new EBMSEntities();
        // GET: api/Login
        public IQueryable<LoginViewModel> GetUsers()
        {
            var users = from user in db.AspNetUsers
                        select new LoginViewModel
                        {
                            Username = user.Email,
                            Password = user.PasswordHash, 
                            COMPANY_ID = user.Company_Id
                        };

            return users;
        }

    }
}

LoginPage。xaml在XamarinPortable

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinFormsDemo.Views.LoginPage"
             BackgroundImage="bg3.jpg"
             Title="MainPage">


  <StackLayout VerticalOptions="Center"
                 Padding="40">
    <Image Source="ebmslogo1.png"/>
    <StackLayout Padding="0,50,0,0">

      <Entry x:Name="txtUserName"
                 Placeholder="Username"
                 x:Hint="Username"
                 BackgroundColor="Black"
                 TextColor="White"/>
      <Entry x:Name="txtPassword"
             Placeholder="Password"
             IsPassword="true"
             BackgroundColor="Black"
             TextColor="White"/>
      <Button Text="LOG IN"
              FontSize="14"
             BackgroundColor="Teal"
             Clicked="NavigateButton_OnClicked"/>

    </StackLayout>
  </StackLayout>

</ContentPage>

loginpage . example .cs in XamarinPortable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace XamarinFormsDemo.Views
{
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);
        }
    }
}

Xamarin的.表单:如何验证登录的用户

我不是WebAPI专家,所以这可能不正确,但基本逻辑应该工作

    public bool AuthUser(string user, string pass)
    {
        // here you will need to hash the password using
        // the same function as when the user was created
        string hash = some_function(pass);
        var user = from user in db.AspNetUsers 
                   where user.Email == user &&
                   user.PasswordHash == hash
                   select user;
        // found a matching user
        if (user != null) return true;
        // did not find a match
        return false;
    }