C# 我正在尝试从azure数据库中为我的xamarin项目获取数据

C# 我正在尝试从azure数据库中为我的xamarin项目获取数据,c#,azure,C#,Azure,我对这一切都很陌生。我正在尝试为我当地的橄榄球俱乐部创建一个桌上服务应用程序。我已将我的应用程序连接到Azure sql数据库,但当我尝试登录时,用户可以注册,但不会发生任何情况。这是我的用户类: using System; namespace LeighRUFCapp.Model { public class Users { // [PrimaryKey, AutoIncrement] public string ID { get; se

我对这一切都很陌生。我正在尝试为我当地的橄榄球俱乐部创建一个桌上服务应用程序。我已将我的应用程序连接到Azure sql数据库,但当我尝试登录时,用户可以注册,但不会发生任何情况。这是我的用户类:

    using System;
namespace LeighRUFCapp.Model
{
    public class Users
    {
        // [PrimaryKey, AutoIncrement]
        public string ID { get; set; }

        //  [MaxLength(256)]
        public string Email { get; set; }
        public string Password { get; set; }
    }
}
下面是我登录页面的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="LeighRUFCapp.LoginPages.LoginPage" xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView">
        <ContentPage.Content>
                <StackLayout BackgroundColor="#313131">
            <Image Source="logo.png" HorizontalOptions="CenterAndExpand" Margin="40" VerticalOptions="Center"></Image>

            <StackLayout Orientation="Vertical" Margin="20" AnchorY="0.5" VerticalOptions="StartAndExpand">
                <Entry Placeholder="Email" TextColor="White" PlaceholderColor="LightGray" Keyboard="Email" x:Name="EmailAddressEntry"></Entry>
                <Entry Placeholder="Password" TextColor="White" PlaceholderColor="LightGray" IsPassword="True" x:Name="UserPasswordEntry"></Entry>

                   <yummy:PancakeView BackgroundColor="#EFCC45" CornerRadius="20,20,20,20" HorizontalOptions="FillAndExpand" HeightRequest="50" Margin="25,0,25,0">
                  <yummy:PancakeView.Shadow>
                    <yummy:DropShadow Color="#232323" Offset="10,10"/>
                </yummy:PancakeView.Shadow>
                 <Button x:Name="LoginButton" Clicked="LoginButton_Clicked" Text="Login" FontSize="Large" HorizontalOptions="FillAndExpand" BorderColor="#000000" BackgroundColor="#EFCC45" FontFamily="helvetica"  TextColor="#000000"></Button>
                 </yummy:PancakeView>

                   <yummy:PancakeView BackgroundColor="#EFCC45" CornerRadius="20,20,20,20" HorizontalOptions="FillAndExpand" HeightRequest="50" Margin="25,0,25,0">
                  <yummy:PancakeView.Shadow>
                    <yummy:DropShadow Color="#232323" Offset="10,10"/>
                </yummy:PancakeView.Shadow>
                <Button x:Name="RegisterBtn" Clicked="RegisterBtn_Clicked"  Text="Register" HorizontalOptions="FillAndExpand" BorderColor="#000000" BackgroundColor="#EFCC45" FontFamily="helvetica" FontSize="Large" TextColor="#000000"></Button>

                 </yummy:PancakeView>

            </StackLayout>

                    <StackLayout>
                        <Button x:Name="Adminbtn" Clicked="Adminbtn_Clicked" Text="Admin" TextColor="#EFCC45" FontFamily="helvetica" FontSize="Subtitle" BackgroundColor="#313131"/>
                    </StackLayout>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Linq;
using LeighRUFCapp.Homestuff;
using LeighRUFCapp.Model;

using Xamarin.Forms;

namespace LeighRUFCapp.LoginPages
{
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            SetValue(NavigationPage.HasNavigationBarProperty, false);

            InitializeComponent();
        }

        private async void LoginButton_Clicked(System.Object sender, System.EventArgs e)
        {


            bool isEmailEmpty = string.IsNullOrEmpty(EmailAddressEntry.Text);
            bool isPasswordEmpty = string.IsNullOrEmpty(UserPasswordEntry.Text);

            if (isEmailEmpty || isPasswordEmpty)
            {
                await DisplayAlert("Error", "Your Email or Password is incorrect", "OK");
            }
            else
            {
                var user = await App.MobileService.GetTable<Users>().Where(u => u.Email == EmailAddressEntry.Text).ToListAsync();




                if (user != null)
                {
                    //if (Users.Password == UserPasswordEntry.Text)
                    //{
                    await Navigation.PushAsync(new HomePage());
                    /* }

                     else
                     {
                         //password is not being found in the table usersfor some reason
                         await DisplayAlert("Error", "Incorrect email or password", "OK");

                     }

                 }

                 else
                 {
                     await DisplayAlert("Error", "There was an error logging you in", "OK");

                 }*/
                }

            }
        }

        void RegisterBtn_Clicked(System.Object sender, System.EventArgs e)
        {
            App.Current.MainPage = new NavigationPage(new RegisterPage());

        }

        void Adminbtn_Clicked(System.Object sender, System.EventArgs e)
        {
            App.Current.MainPage = new NavigationPage(new AdminLogin());
        }
    }
}

我的c#用于登录页面

<?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="LeighRUFCapp.LoginPages.LoginPage" xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView">
        <ContentPage.Content>
                <StackLayout BackgroundColor="#313131">
            <Image Source="logo.png" HorizontalOptions="CenterAndExpand" Margin="40" VerticalOptions="Center"></Image>

            <StackLayout Orientation="Vertical" Margin="20" AnchorY="0.5" VerticalOptions="StartAndExpand">
                <Entry Placeholder="Email" TextColor="White" PlaceholderColor="LightGray" Keyboard="Email" x:Name="EmailAddressEntry"></Entry>
                <Entry Placeholder="Password" TextColor="White" PlaceholderColor="LightGray" IsPassword="True" x:Name="UserPasswordEntry"></Entry>

                   <yummy:PancakeView BackgroundColor="#EFCC45" CornerRadius="20,20,20,20" HorizontalOptions="FillAndExpand" HeightRequest="50" Margin="25,0,25,0">
                  <yummy:PancakeView.Shadow>
                    <yummy:DropShadow Color="#232323" Offset="10,10"/>
                </yummy:PancakeView.Shadow>
                 <Button x:Name="LoginButton" Clicked="LoginButton_Clicked" Text="Login" FontSize="Large" HorizontalOptions="FillAndExpand" BorderColor="#000000" BackgroundColor="#EFCC45" FontFamily="helvetica"  TextColor="#000000"></Button>
                 </yummy:PancakeView>

                   <yummy:PancakeView BackgroundColor="#EFCC45" CornerRadius="20,20,20,20" HorizontalOptions="FillAndExpand" HeightRequest="50" Margin="25,0,25,0">
                  <yummy:PancakeView.Shadow>
                    <yummy:DropShadow Color="#232323" Offset="10,10"/>
                </yummy:PancakeView.Shadow>
                <Button x:Name="RegisterBtn" Clicked="RegisterBtn_Clicked"  Text="Register" HorizontalOptions="FillAndExpand" BorderColor="#000000" BackgroundColor="#EFCC45" FontFamily="helvetica" FontSize="Large" TextColor="#000000"></Button>

                 </yummy:PancakeView>

            </StackLayout>

                    <StackLayout>
                        <Button x:Name="Adminbtn" Clicked="Adminbtn_Clicked" Text="Admin" TextColor="#EFCC45" FontFamily="helvetica" FontSize="Subtitle" BackgroundColor="#313131"/>
                    </StackLayout>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>
using System;
using System.Collections.Generic;
using System.Linq;
using LeighRUFCapp.Homestuff;
using LeighRUFCapp.Model;

using Xamarin.Forms;

namespace LeighRUFCapp.LoginPages
{
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            SetValue(NavigationPage.HasNavigationBarProperty, false);

            InitializeComponent();
        }

        private async void LoginButton_Clicked(System.Object sender, System.EventArgs e)
        {


            bool isEmailEmpty = string.IsNullOrEmpty(EmailAddressEntry.Text);
            bool isPasswordEmpty = string.IsNullOrEmpty(UserPasswordEntry.Text);

            if (isEmailEmpty || isPasswordEmpty)
            {
                await DisplayAlert("Error", "Your Email or Password is incorrect", "OK");
            }
            else
            {
                var user = await App.MobileService.GetTable<Users>().Where(u => u.Email == EmailAddressEntry.Text).ToListAsync();




                if (user != null)
                {
                    //if (Users.Password == UserPasswordEntry.Text)
                    //{
                    await Navigation.PushAsync(new HomePage());
                    /* }

                     else
                     {
                         //password is not being found in the table usersfor some reason
                         await DisplayAlert("Error", "Incorrect email or password", "OK");

                     }

                 }

                 else
                 {
                     await DisplayAlert("Error", "There was an error logging you in", "OK");

                 }*/
                }

            }
        }

        void RegisterBtn_Clicked(System.Object sender, System.EventArgs e)
        {
            App.Current.MainPage = new NavigationPage(new RegisterPage());

        }

        void Adminbtn_Clicked(System.Object sender, System.EventArgs e)
        {
            App.Current.MainPage = new NavigationPage(new AdminLogin());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用LeighRUFCapp.Homestuff;
采用LeighRUFCapp.Model;
使用Xamarin.Forms;
命名空间LeighRUFCapp.LoginPages
{
公共部分类登录页:ContentPage
{
公共登录页()
{
SetValue(NavigationPage.HasNavigationBarProperty,false);
初始化组件();
}
已单击专用异步void登录按钮(System.Object sender,System.EventArgs e)
{
bool isEmailEmpty=string.IsNullOrEmpty(EmailAddressEntry.Text);
bool isPasswordEmpty=string.IsNullOrEmpty(UserPasswordEntry.Text);
如果(isEmailEmpty | | isPasswordEmpty)
{
等待DisplayAlert(“错误”、“您的电子邮件或密码不正确”、“确定”);
}
其他的
{
var user=wait App.MobileService.GetTable().Where(u=>u.Email==EmailAddressEntry.Text).toListSync();
如果(用户!=null)
{
//if(Users.Password==UserPasswordEntry.Text)
//{
等待导航。PushAsync(新主页());
/* }
其他的
{
//由于某些原因,在Users表中找不到密码
等待显示警报(“错误”、“不正确的电子邮件或密码”、“确定”);
}
}
其他的
{
等待显示警报(“错误”,“登录时出错”,“确定”);
}*/
}
}
}
已单击无效注册表项(System.Object sender,System.EventArgs e)
{
App.Current.MainPage=新导航页面(新注册页面());
}
已单击void Adminbtn_(System.Object sender,System.EventArgs e)
{
App.Current.MainPage=新导航页面(新AdminLogin());
}
}
}
我的建议是:

  • 将数据检索置于服务之后(WebAPI)
  • 从移动应用程序调用该服务
  • 这更安全,是移动应用程序的常见模式。通常,您将使用JSON作为响应类型来设计RESTAPI。这将意味着在启动Xamarin应用程序之前,在VisualStudio中通过右键单击来启动服务(假设是Android,如果不是,你最终需要一台Mac)。或者,您可以单独执行该服务的一个实例。一旦调试完成,VS就可以很容易地部署到Azure云

    你能远程访问数据库吗?是的,但是您必须关闭一些安全功能,或者通过HTTPS或类似方式使用访问。这不是一个很好的模式,因为最终在应用程序中会出现连接字符串。并不是说移动应用程序很容易通过逆向工程获得信息,而是建议采用以下最佳做法。

    我的建议是:

  • 将数据检索置于服务之后(WebAPI)
  • 从移动应用程序调用该服务
  • 这更安全,是移动应用程序的常见模式。通常,您将使用JSON作为响应类型来设计RESTAPI。这将意味着在启动Xamarin应用程序之前,在VisualStudio中通过右键单击来启动服务(假设是Android,如果不是,你最终需要一台Mac)。或者,您可以单独执行该服务的一个实例。一旦调试完成,VS就可以很容易地部署到Azure云

    你能远程访问数据库吗?是的,但是您必须关闭一些安全功能,或者通过HTTPS或类似方式使用访问。这不是一个很好的模式,因为最终在应用程序中会出现连接字符串。并不是说移动应用程序很容易通过逆向工程获得信息,而是建议遵循最佳实践