2016-08-17 147 views
0

好日子大家。我正在创建一个简单的Xamarin.Forms Portable。截至目前,我正在创建一个登录功能,允许所有注册用户登录到我的系统。Xamarin.Forms:如何验证用户登录?

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

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

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

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

下面是我的一些代码:

LoginController.cs中的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.xaml.cs在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); 

     } 

    } 
} 
+0

你需要创建一个在您的WebAPI一个登录方法接受一个用户名和密码,如果值与数据库中的用户相匹配,则返回true。不要将用户及其哈希列表返回给客户端 - 这是非常不安全的。 – Jason

+0

@Jason先生如何在我的WEB API中创建一个接受用户名和密码的Login方法,并在值与数据库中的用户匹配时返回true? –

回答

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; 
    } 
+0

我应该做这在我的ApiController? –