hi, i have pb wt Active Directory authentication:
1) 1st try: Membership.GetUser return NULL
2) 2nd try: DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
SearchResult result = search.FindOne(); go to catch exception
S.O can help, pls? Here's my code:
1st try :
protected void LoginButton_Click(object sender, EventArgs e)
{
DirectoryEntry entry = new DirectoryEntry("LDAP://Media.local/DC=Media, DC=local", "media\\tng", "****");
object connect = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(&(objectCategory=user)(SAMAccountName=*))";
search.PropertiesToLoad.Add("cn");
foreach (SearchResult result in search.FindAll())
{
DirectoryEntry dirEntry = result.GetDirectoryEntry();
string login = (dirEntry.Properties["SAMAccountName"].Value !=
null) ? dirEntry.Properties["SAMAccountName"].Value.ToString() :
String.Empty;
string name = (dirEntry.Properties["sn"].Value != null) ? dirEntry.Properties["sn"].Value.ToString() : String.Empty;
string surname = (dirEntry.Properties["givenName"].Value != null)
? dirEntry.Properties["givenName"].Value.ToString() : String.Empty;
string tel = (dirEntry.Properties["TelephoneNumber"].Value !=
null) ? dirEntry.Properties["TelephoneNumber"].Value.ToString() :
String.Empty;
string email = (dirEntry.Properties["mail"].Value != null) ?
dirEntry.Properties["mail"].Value.ToString() : String.Empty;
string path = result.Path;
string filterAttribute = (String)result.Properties["cn"][0];
if (String.Compare(login, 0, "tng", 0, 3, true) == 0)
{
lErreur.Text = " login=" + login + " name=" + name + " surname=" + surname + " tel=" + tel + " email=" + email;
if (Request.LogonUserIdentity.IsAuthenticated)
lErreur.Text += " Request.LogonUserIdentity.IsAuthenticated=" + Request.LogonUserIdentity.Name;
lErreur.Text += " User.Identity.IsAuthenticated=" + User.Identity.IsAuthenticated;
// the debug skips the if case. In the label: login=TNG name=NGO
surname=Thanh tel=01 00 00 00 00 email=thanh.ngo@media.com
User.Identity.IsAuthenticated=False
MembershipUser u = Membership.GetUser(Login1.UserName); //Login1.UserName=”tng”
if (u == null) //u=null. WHY???
{
Login1.FailureText = "Invalid user name. Please check your user name and try again.";
return;
}
if (Membership.ValidateUser(Login1.UserName.ToString(), Login1.Password.ToString()))
{
FormsAuthentication.RedirectFromLoginPage(Login1.UserName.ToString(), false);
HttpContext.Current.Session["username"] = Login1.UserName.ToString();
Response.Redirect("Welcome.aspx",false);
}
else
Login1.FailureText = "Invalid password. Please check your password and try again.";
break;
}
}
}
2nd try: I clear all & try the method proposed by msdn:
public partial class Identification : System.Web.UI.Page
{
private string _path;
private string _filterAttribute;
public Identification(string path)
{
_path = path;
}
public bool IsAuthenticated(string domain, string username, string pwd)
{
string domainAndUsername = domain + username;
DirectoryEntry entry = new DirectoryEntry( _path, domainAndUsername, pwd);
try
{
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
public string GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")"; //search.filter=”Thanh NGO”
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne(); //aller dans catch(exception ex). Pourquoi???
int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;
for( int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch(Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
protected void LoginButton_Click(object sender, EventArgs e)
{
string adPath = "LDAP://Media.local/DC=Media,DC=local";
Identification adAuth = new Identification(adPath);
try
{
if (true == adAuth.IsAuthenticated("Media\\",
Login1.UserName.ToString(),Login1.Password.ToString()))
{
string groups = adAuth.GetGroups(); //aller dans catch(exception ex).Regarde fct
//GetGroups() en haut
FormsAuthenticationTicket authTicket =
new FormsAuthenticationTicket(1,
Login1.UserName.ToString(),DateTime.Now,
DateTime.Now.AddMinutes(60),false, groups);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new Cookie(FormsAuthentication.FormsCookieName,
encryptedTicket);
Response.Cookies.Add(authCookie);
Response.Redirect("Welcome.aspx",false)Response.Redirect("Welcome.aspx",false);
}
else
{
lErreur.Text =
"Authentication failed, check username and password.";
}
}
catch(Exception ex)
{
lErreur.Text = "Error authenticating. " + ex.Message;
}
}
}
here's my web.config for both try:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ADMedia" connectionString="LDAP://Media"/>
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms loginUrl="login.aspx" path="/" requireSSL="false"
cookieless="UseDeviceProfile" enableCrossAppRedirects="false"
protection="All" domain=http://Media name=".ASPXFORMSAUTH">
</forms>
</authentication>
<membership defaultProvider="AspNetActiveDirectoryMembershipProvider" >
<providers>
<remove name="AspNetActiveDirectoryMembershipProvider"/>
<add connectionStringName="ADMedia"
connectionUsername="media\tng" connectionPassword="****"
name="AspNetActiveDirectoryMembershipProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider,System.Web,version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</membership>
<roleManager enabled="true"/>
<anonymousIdentification enabled="true"/>
…
Média is a Outlook LDAP (wich is in another pc & linked to mine
by intranet) where there's every information (name, surname, email,
tel, group, @pc).
I configured IIS like this : in directory security :
- check Anonymous Connection
- uncheck Authorise psw verification by IIS
- check base authentification, default domain : Media, domain: Media.local
- check window integreted authentification
I can't connect to management of ASP.NET via SiteWeb menu in
VisualStudio2008. Error: “Provider Management Could not establish a
connection to the database. If you have not yet created the SQL Server
database, exit the Web Site Administration tool, use the aspnet_regsql
command-line utility to create and configure the database, and then
return to this tool to set the provider.”
I executed aspnet_regsql from the command line, nothing changed. What else I have to do? Thank you for your help