using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Net;
using Microsoft.Crm.Services.Utility;
using System.ServiceModel.Description;
using System.Security.Cryptography;
using Xrm;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using System.Data.SqlClient;
namespace ToolsCRM
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        

        protected void Polacz_Click(object sender, EventArgs e)
        {

            //string UserName = username.Text;   
            //string Password = password.Text;

            //var connection = CrmConnection.Parse("Url=https://testcrmmd.api.crm4.dynamics.com; Username=Test@TestCRMMD.onmicrosoft.com; Password=50.qwaszx.50;");
 
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            var xrm = new XrmServiceContext("Xrm");
            var query =
                (from u in xrm.new_uzytkownicySet
                 where u.new_name == TextBoxLogin.Text
                 select u).ToList();
            int temp_checkUser = query.Count();

            if (temp_checkUser == 1)
            {
                var query2 =
                    (from u in xrm.new_uzytkownicySet
                     where u.new_name == TextBoxLogin.Text
                     select u.new_haslo).Single();
            
                string pass_temp = query2;
          
                if (pass_temp == TextBoxPassw.Text.Trim())
                {
                    Session["user"] = TextBoxLogin.Text;
                    Response.Redirect("user.aspx");
                }
                else
                {                    
                    Label2.Visible = true;
                    Label2.Text = "Niepoprawne hasło";
                }
            }
            else
            {
                Label2.Visible = true;
                Label2.Text = "Niepoprawny login";
            }
        }    
    }
}