2016-08-17 26 views
0

おはようございます。私はシンプルなXamarin.Forms Portableを作成しています。今のところ、登録されたすべてのユーザーが自分のシステムにログインできるようにするLogin機能を作成しています。Xamarin.Forms:ログインしているユーザーを認証する方法は?

WEB APIを使用して、すべてのユーザー情報(ユーザー名とパスワード)を取得できました。

私がしたいのは、ユーザーがユーザー名とパスワードのテキストボックスにデータを入力するたびに、システムはその値を取得し、WEB APIによって収集されたレコードにキー付きデータが存在するかどうかを確認する必要があります。

存在する場合、ユーザーはシステムにログインしなければなりません。

あなたは私の状況を理解したいと考えています。どうもありがとう。

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

<?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にWebフォームで

LoginController.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

動作するはずですし、値がdb内のユーザーと一致する場合はtrueを返します。ユーザーのリストとそのハッシュをクライアントに返さないでください。これはひどく不安です。 – Jason

+0

@Jason Sirユーザー名とパスワードを受け入れ、値がデータベースのユーザーと一致する場合はtrueを返す、WEB APIでLoginメソッドを作成するにはどうすればよいですか? –

答えて

1

私は、これは正しくない可能性がありますWebAPIの専門家ではないけど、基本的なロジックを使用して、ユーザー名とパスワードを受け入れ、あなたの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でこれを行う必要がありますか? –

関連する問題