Hi,
I am developing an MVC3 application. It will be an Intranet System andauthenticate using Windows.
If a user belongs to the NetWork of the Company Building; the usershould be Authenticated and allowed to see the pages.
1. What kind of authentication should I use?
2. I am using IIS6 in my hosting server, what changes should I make there?
3. Any sample MVC3(C#) will be helpful as well?
I did as it says in "To configure the Visual Studio Development Server for Windows Authentication" of
msdn.microsoft.com/en-us/library/gg703322(v=vs.98).aspx and then I set my web.config -
It works for my local machine.
When I run the project it asks for my Windows account details. B
ut, after I moved to my Test Server (IIS6), when I ran the project, it DOES NOT ask for account details or it DOES NOT login by default.
Thanks very much
Scott LyslePosted Jul 10, 2013, 8:36 PM
After you have their information, you can authenticate them straight away with this:
// Get the currently logged in person's user name. (with the domain name)
public static string GetDomainUserName()
{
return System.Web.HttpContext.Current.Request.LogonUserIdentity.Name;
}
// Get just the currently logged in person's user name (without the domain name)
public static string GetShortUserName()
{
string rtnVal = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name;
string[] parts = rtnVal.Split('\\');
rtnVal = parts[parts.Length - 1]; // return only the last part - that's the user name
return rtnVal;
}
// Get just the currently logged in person's domain name (without the user name)
public static string GetDomainName()
{
string rtnVal = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name;
string[] parts = rtnVal.Split('\\');
rtnVal = parts[0]; // return only the last part - that's the user name
return rtnVal;
}
///
/// Determine if user Id and password represent a valid Active Directory User
/// This method assumes the execution is within the same domain as the tested
/// user - not sure if this can always be relied upon so the next function
/// performs the same test but allow passing in the domain name as an argument
///
///
///
///
public static bool ValidateActiveDirUser(string userId, string password)
{
PrincipalContext adContext = new PrincipalContext(ContextType.Domain);
using (adContext)
return adContext.ValidateCredentials(userId, password);
}
///
/// Validate a user is a valid AD member in a specified domain
///
/// Provide the user's user name (ID)
/// Provide the user's password (plain text)
/// Provide the user's domain name
///
public static bool ValidateActiveDirDomainUser(string userId, string password, string domainName)
{
PrincipalContext prncContext = new PrincipalContext(ContextType.Domain, domainName);
using (prncContext)
return prncContext.ValidateCredentials(userId, password);
}