2016-08-17 67 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? –