我能够使用WEB API检索所有用户信息(用户名和密码)。
我想要做的是,只要用户在我的用户名和密码文本框中输入数据,系统就应该获取它的值,并检查我的WEB API收集的记录上是否存在该键控数据。
如果存在,则用户应登录系统,否则不登录。
希望你了解我的情况。非常感谢。
以下是我的一些代码:
WebForms中的LoginController.cs
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;
}
}
}
XamarinPortable中的LoginPage.xaml
<?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>
XamarinPortable中的LoginPage.xaml.cs
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);
}
}
}
答案 0 :(得分:1)
我不是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;
}