0
Reply

How to login to one admin account from admin multiple accounts in windows based on credentails and with out match each account credentails using c#.net code

raheemvali shaik

raheemvali shaik

Aug 9 2011 6:07 AM
1.5k
My Problem is iam created three accounts under admin in my system(all three create under systm admin).Those are Account1:admin1 Account2:TestUser1 Account3:TestUser2. Now iam login from each account based on that account credentails using c# code and run my c# application. The Problem is the application supports all thee accounts credentails when iam login from each account.Forexample Now iam login from TestUser1 it is working well by testuser1 credentails ,but it also suport admin1,TestUser2 credentails. My problem is i want login based on that account credentails only not from other two. iam working on c#windows application. This application Creating for Shift system for multiple users login and working from their own accounts.
Hello Anybody help me plz.. on this problem.
My login code is


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;


namespace Transaction
{
    public partial class FrmLogIn : Form
    {
        public  string TrmsUserLoginName;
        [DllImport("ADVAPI32.dll", EntryPoint = "LogonUserW", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
       
     
        public FrmLogIn()
        {
            InitializeComponent();
        }


        private void FrmLogIn_Load(object sender, EventArgs e)
        {
            foreach (Control ctl in pnlLogin.Controls)
            {
                if (ctl.TabIndex == 1)
                    ctl.Focus();
            }
        }
       


        private void btnOK_Click(object sender,System. EventArgs e)
        {
            toolStripStatusLabel1.Text = this.showstatus(" Login plz wait Verify the credentails...");


            System.Threading.Thread.Sleep(4000);
            string domainName = GetDomainName(txtUserName.Text); // Extract domain name form provide DomainUsername e.g Domainname\Username
            string userName = GetUsername(txtUserName.Text);  // Extract user name from provided DomainUsername e.g Domainname\Username
            IntPtr token = IntPtr.Zero;
           
           
            bool result = LogonUser(userName, domainName, txtPassword.Text, 2, 0, ref token);
            if (result)
            {
               
                MessageBox.Show("LOGIN SUCCESSFULLY ");
                TrmsUserLoginName = txtUserName.Text;
                FrmTRMS obj = new FrmTRMS(TrmsUserLoginName);                                      
                obj.Show();                
                this.Hide();


            }
             




            //if (txtUserName.Text == "KITS" & txtPassword.Text == "KITS")
            //{
            //    // mainform.Show();
               
            //}


            else if (string.IsNullOrEmpty(txtUserName.Text))
            {
                txtUserName.Focus();
                MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserName.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(txtPassword.Text))
            {
                txtPassword.Focus();
                MessageBox.Show("Enter Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);


                txtPassword.Focus();
                return;
            }
            else
            {
                txtUserName.Text = "";
                txtPassword.Text = "";
                MessageBox.Show("Invalid Login");
                txtUserName.Focus();
            }


            toolStripStatusLabel1.Text = this.showstatus("LOgin Successfully");
        }
        public string showstatus(string status)
        {
            //timer1.Enabled = true;
            this.toolStripStatusLabel1.Text = status;
            Application.DoEvents();
            return status;


        }
        public static string GetDomainName(string usernameDomain)
        {
            if (string.IsNullOrEmpty(usernameDomain))
            {
                throw (new ArgumentException("Argument can't be null.", "usernameDomain"));
            }
            if (usernameDomain.Contains("\\"))
            {
                int index = usernameDomain.IndexOf("\\");
                return usernameDomain.Substring(0, index);
            }
            else if (usernameDomain.Contains("@"))
            {
                int index = usernameDomain.IndexOf("@");
                return usernameDomain.Substring(index + 1);
            }
            else
            {
                return "";
            }
        }
        public static string GetUsername(string usernameDomain)
        {
            if (string.IsNullOrEmpty(usernameDomain))
            {
                throw (new ArgumentException("Argument can't be null.", "usernameDomain"));
            }
            if (usernameDomain.Contains("\\"))
            {
                int index = usernameDomain.IndexOf("\\");
                return usernameDomain.Substring(index + 1);
            }
            else if (usernameDomain.Contains("@"))
            {
                int index = usernameDomain.IndexOf("@");
                return usernameDomain.Substring(0, index);
            }
            else
            {
                return usernameDomain;
            }
        }




       
    }
}