简体   繁体   中英

Automatic login for Forms authenticated MVC app based on Windows username, possible?

I have an asp.net mvc application using forms authentication ie users/passwords are in a database. This application is used by several customers on their own servers and one would like their staff to be logged in automatically as per Intranet because apparently having to login would stop them from using it!?!

I am sure you can't mix both Windows and Forms authentication so my question is, is there anyway to identify the windows/ad username or anything else in anyway so I can authenticate them manually? I don't need to actually authenticate them via windows I just need to somehow get information that will allow me to match them to the record in the database.

I've done something similar to this. You will need to configure IIS to have Windows Authentication enabled. If their AD account matches their identification for the web application(or just add a column for AD id in your forms authentication database), you can strip the domain from their username and then validate it against your authentication database.

I have an HR database that we use to keep track of employees and their titles, based on the data contained in this database we allow certain privileges throughout the application:

Our company is still on active directory 2003 so it was impossible to manage credentials without finding a creative solution.

Here is my code: //find out who the Active Directory user is var username = Convert.ToString(User.Identity.Name);

    // strip the domain
    username = Regex.Replace(username, "DOMAIN", "");
    username = Regex.Replace(username, "\\\\", "");


    var first_initial = username[0];
    var last_initial = username[1];
    //The Ative directory structure is [first initial firstname][first inital lastname][badge number]
    var EIN = Regex.Replace(username, "[^0-9]", "");//get the badge number so we can query the badging database


    // check the employee database to see if this AD account matches something 
    var query = "SELECT BadgeNumber, FirstName, LastName,  DEPT_NUM, DEPT, TITLE, Supervisor " +
                     "FROM TABLE_WITH EMPLOYEE INFORMATION " +
                     "WHERE LEFT(FirstName, 1) = '" + first_initial + "' AND LEFT(LastName, 1) = '" + last_initial + "' AND BadgeNumber = '" + EIN + "' AND STATUS = 'Active' ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM