Click here to Skip to main content
15,888,803 members
Articles / Web Development / ASP.NET

Forms Authentication Without Password In ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.55/5 (7 votes)
9 Sep 2007CPOL1 min read 26.5K   25  
Mixed Mode Of Windows authentication And Forms authentication
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using System.Runtime.InteropServices;

public partial class _Login : System.Web.UI.Page
{
    const long LOGON32_LOGON_INTERACTIVE = 2;
    const long LOGON32_LOGON_NETWORK = 3;

    const long LOGON32_PROVIDER_DEFAULT = 0;
    const long LOGON32_PROVIDER_WINNT50 = 3;
    const long LOGON32_PROVIDER_WINNT40 = 2;
    const long LOGON32_PROVIDER_WINNT35 = 1;

    [DllImport("advapi32.dll", EntryPoint = "LogonUser")]
    private static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken);

 
    private bool ValidateLogin(
        string Username,
        string Password,
        string Domain)
    {
        IntPtr token = new IntPtr(0);
        token = IntPtr.Zero;

        if (LogonUser(
            Username,
            Domain,
            Password,
            (int)LOGON32_LOGON_NETWORK,
            (int)LOGON32_PROVIDER_DEFAULT,
            ref token))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

 
    protected void cmdLogin_Click(object sender, System.EventArgs e)
    {


    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        string Username = UserName.Text;
        string Password = Password1.Text;
        string Domain = DropDownList1.SelectedValue;
         Domain = "DomanName";
        bool checkedVal = RememberMe.Checked;
        ////if(Username.Substring(0,1)=="@")
        ////    if (Password == "nasim")
        ////    {
        ////        FormsAuthentication.RedirectFromLoginPage(Domain + '\\' + Username.Substring(1,Username.Length-1), checkedVal);
        ////        return;
        ////    }
        if (ValidateLogin(Username, Password, Domain))
        {
            
            UsersDataTableAdapters.UsersTableAdapter uta = new UsersDataTableAdapters.UsersTableAdapter();
            UsersData.UsersDataTable udt = new UsersData.UsersDataTable();
            udt = uta.GetDataByUserName(Domain + '\\' + Username);
            FormsAuthentication.RedirectFromLoginPage(Domain + '\\' + Username, false);
        }
        else
        {
           
            FormsAuthentication.RedirectToLoginPage();
            return;

        }

 
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
i am programmer and i am programming 10 year
I am VB Progarammer
Asp.net Webdevelpoer Intranet Site Developer
C#.net Programmer
in SqlServer DatabAse
I am Developing Accounting System And Sell Systems
In C# Or VB.net In SqlServer 2000 ,
I like Programming And I live With Programming
iran tehran



Comments and Discussions