Exception while getting sAMAccountName from DirectroryEntry - c#

I am using DirectorySearcher to get AD groups from LDAP. The below code works when I fetch the property "cn" from DirectoryEntry and it throws an exception when "sAMAccountName" is selected. The property does exist as you can see that I search based on sAMAccountName.
Exception is
"Exception Details: System.Runtime.InteropServices.COMException:
Unknown error (0x8000500c)"
[COMException (0x8000500c): Unknown error (0x8000500c)]
System.DirectoryServices.PropertyValueCollection.PopulateList()
+519959 System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry
entry, String propertyName) +119
System.DirectoryServices.PropertyCollection.get_Item(String
propertyName) +162
ASP._Page_app_tools_active_directory_cshtml.SearchADGroups(List1 Fields, String DomainName) in d:\inetpub\wwwroot\app\tools\active_directory.cshtml:371 ASP._Page_app_tools_active_directory_cshtml.Execute() in d:\inetpub\wwwroot\app\tools\active_directory.cshtml:154 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +252 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable1
executors) +99 System.Web.WebPages.WebPage.ExecutePageHierarchy()
+182 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext
pageContext, TextWriter writer, WebPageRenderingBase startPage) +107
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase
httpContext) +142
List<ADGroup> Groups = new List<ADGroup>();
using (DirectoryEntry dEntry = new DirectoryEntry("LDAP://" + DomainName))
{
using (DirectorySearcher gSearch = new DirectorySearcher())
{
gSearch.SearchRoot = dEntry;
if(GroupName != "")
{
gSearch.Filter = String.Format("(&(objectClass=group)(samaccountname=*{0}*))", GroupName);
gSearch.SearchScope = SearchScope.Subtree;
gSearch.PropertiesToLoad.Add("sAMAccountName");
SearchResultCollection group_results = gSearch.FindAll();
foreach (SearchResult group_result in group_results)
{
if(group_result != null)
{
DirectoryEntry group_entry = group_result.GetDirectoryEntry();
group_entry.Properties["cn"].Value.ToString()
group_entry.Properties["sAMAccountName"].Value.ToString()
}
}
}
}
}
I expect the value for sAMAccountName

Related

Random exception on Web service call

I have a Web service which works fine in production environment.
But sometimes (randomly) an exception is raised :
à System.Threading.Tasks.Task.ThrowIfExceptional(Boolean
includeTaskCanceledExceptions)\r\n à
System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)\r\n à System.Threading.Tasks.Task1.get_Result()\r\n à
fctSendRequestSynchrone[T](String sRequest, enumHttpMethod eMethod,
Object oParams)\r\n à API.csRestApi.d__0`1.MoveNext()"
Here is my code :
.........
//Here is the line which raises the exception :
fctSendRequestSynchrone<string>(string.Format("logs/{0}/message", _lIdLog), cs.enumHttpMethod.POST, oLogLigne);
.........
//-------------------------------------------------------------------------------------
private T fctSendRequestSynchrone<T>(string sRequest, csRestApi.enumHttpMethod eMethod, object oParams = null)
{
Task<T> otask = SendRequest<T>(sRequest, eMethod, oParams);
return otask.Result;
}
//-------------------------------------------------------------------------------------
public async Task<T> SendRequest<T>(string sAction, enumHttpMethod eMethod, object oParams = null)
{
string sResponse = string.Empty;
T oValue;
using (var oClient = new HttpClient(new LogginHandler(_oCnx, new HttpClientHandler())))
{
oClient.DefaultRequestHeaders.Accept.Clear();
oClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string sRequest = string.Concat(_sUrlApi, "/", sAction);
if (_oToken != null)
{
oClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_oToken["token_type"], _oToken
["access_token"]);
}
using (HttpResponseMessage oResponse = await oClient.PostAsJsonAsync(sRequest, oParams))
{
if (oResponse.IsSuccessStatusCode)
{
HttpContent content = oResponse.Content;
sResponse = await content.ReadAsStringAsync();
}
else
{
throw new RestApiException(oResponse);
}
}
}
oValue = JsonConvert.DeserializeObject<T>(sResponse);
return oValue;
}
Do you have an idea ?
Thank you very much in advance.
Eric
Roman Kalinchuk, Crowcoder : thanks for your replies.
Roman Kalinchuk :
Here is the entire stacktrace :
à System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n à System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)\r\n à System.Threading.Tasks.Task`1.get_Result()\r\n à logs.csLogWS.fctSendRequestSynchrone[T](String sRequest, enumHttpMethod eMethod, Object oParams)\r\n à logs.csLogWS.sbrErrorLigneRejet(String sMessage, String sCdRejet, Exception oException)\r\n à logs.csLogWS.ligneLogInformation(String sMessage)\r\n à Importation.Program.sbrImportUnitaire(String sCdInfImp, String sFileNameIn, String sFileStructure, String sLiProcedure, String[] args)| à Service.API.csRestApi.<SendRequest>d__0`1.MoveNext()".
I simplified it in my preceding question post.
The application is on production environment, do I have to put pdb files on production server in order to get more stacktrace ?
Crowcoder : do you mean that .Result is waiting while SendRequest is waiting too ? What would you suggest ?

The ContinuousProcessing parameter is no longer supported.NPGSQL

I just upgraded my npgsql from 2.5.5 to 3.1.1 and i am getting this runtime error.
My DbConnection class
public static string ServerConnectionString()
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
{
Host = ConfigurationManager.AppSettings["Server"],
Username = ConfigurationManager.AppSettings["UserId"],
Database = "postgres",
Password = ConfigurationManager.AppSettings["Password"],
CommandTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["CommandTimeout"]),
ApplicationName = EverestEnums.ConnectionApplicationName.EverestServerChecker.ToString(),
// MaxPoolSize = 200,
// SyncNotification = true,
KeepAlive = 1,
ConnectionLifeTime = 1,
// MinPoolSize = 1,
Pooling = false
};
return connectionStringBuilder.ConnectionString;
}
Stack Trace
[NotSupportedException: The ContinuousProcessing parameter is no longer supported. Please see http://www.npgsql.org/doc/3.1/migration.html]
Npgsql.NpgsqlConnectionStringBuilder.set_ConnectionLifeTime(Int32 value) +62
Everest.Net.DatabaseLayer.DBFactory.DbConnection.ServerConnectionString() in E:\Everest_PES\Everest.Net.DatabaseLayer\DBFactory\DBConnection.cs:13
Everest.Net.DatabaseLayer.DBFactory.DBOperations.IsServerAvailable() in E:\Everest_PES\Everest.Net.DatabaseLayer\DBFactory\DBOperations.cs:216
Everest.Net.DatabaseLayer.DBFactory.ServerConnectivity.IsDbServerAvailable() in E:\Everest_PES\Everest.Net.DatabaseLayer\DBFactory\ServerConnectivity.cs:7
Everest.Net.BusinessLayer.DBFactory.ServerConnectivity.IsDbServerAvailable() in E:\Everest_PES\Everest.Net.BusinessLayer\DBFactory\ServerConnectivity.cs:12
Everest.Net.Web.Global.Application_Start(Object sender, EventArgs e) in E:\Everest_PES\Everest.Net.Web\Global.asax.cs:15
[HttpException (0x80004005): The ContinuousProcessing parameter is no longer supported. Please see http://www.npgsql.org/doc/3.1/migration.html]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +544
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +186
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +402
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +343
[HttpException (0x80004005): The ContinuousProcessing parameter is no longer supported. Please see http://www.npgsql.org/doc/3.1/migration.html]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +579
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +112
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +712
As it was working fine before.But there was some issue with connection pooling.And then i upgraded npgsql and this rumtime error.Any Help.
It looks like that exception is thrown when you get or set the ConnectionLifeTime property on the NpgsqlConnectionStringBuilder. The property is obsolete and replaced by the Connection Idle Lifetime property now (see http://www.npgsql.org/doc/migration.html). Try to use this instead:
public static string ServerConnectionString()
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
{
Host = ConfigurationManager.AppSettings["Server"],
Username = ConfigurationManager.AppSettings["UserId"],
Database = "postgres",
Password = ConfigurationManager.AppSettings["Password"],
CommandTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["CommandTimeout"]),
ApplicationName = EverestEnums.ConnectionApplicationName.EverestServerChecker.ToString(),
// MaxPoolSize = 200,
// SyncNotification = true,
KeepAlive = 1,
ConnectionIdleLifetime = 1,
// MinPoolSize = 1,
Pooling = false
};
return connectionStringBuilder.ConnectionString;
}

UserPrincipal with context that uses SSL

I have a PrincipalContext that uses SSL. This works fine when using a method like Context.ValidateCredentials(). But when I need to find a user using UserPrincipal.FindByIdentity() I get the following error:
System.Runtime.InteropServices.COMException: The server is unwilling to process the request.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_SchemaEntry()
at System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de)
at System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options)
at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
--- End of inner exception stack trace ---
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue)
My method:
public List<string> GetUserInfo(string user) {
var list = new List<string>();
using (var context = new PrincipalContext(ContextType.Domain, "xxxx.xxxx.xxxx:636", "DC=xxxx,DC=xxxx,DC=xxxx", ContextOptions.SimpleBind | ContextOptions.Sealing | ContextOptions.SecureSocketLayer)) {
var uP = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, user);
//Do stuff with uP
return list;
}
But this is working fine:
public bool ValidateCredentials(string username, string password) {
using (var context = new PrincipalContext(ContextType.Domain, "xxxx.xxxx.xxxx:636", "DC=xxxx,DC=xxxx,DC=xxxx", ContextOptions.SimpleBind | ContextOptions.Sealing | ContextOptions.SecureSocketLayer)) {
return context.ValidateCredentials(username, password);
}
}
How come I cant work with UserPrincipal using the Context with SSL? If I remove SSL it works fine..
I changed my ContextOptions to Negotiate and SSL. Then it worked
Unfortunately there are not enough code examples that show how to configure PrincipalContext or DirectoryEntry to use LDAPS (SSL Active Directory). I have found these solutions for this issue:
Configure PrincipalContext to use LDAPS:
var path = "test.domainName.local:636";
ContextOptions options = ContextOptions.Negotiate | ContextOptions.SecureSocketLayer;
using (var context = new PrincipalContext(ContextType.Domain, path, "DC=xyz,DC=local", options))
{
pr("Name: " + context.Name);
pr("ConnectedServer: " + context.ConnectedServer);
pr("Container: " + context.Container);
pr("UserName: " + context.UserName);
}
Configure DirectoryEntry to use LDAPS:
string path = "LDAP://test.domainName.local:636";
var dic = new DirectoryEntry(path);
pr("Name: " + dic.Name);
pr("Path: " + dic.Path);
pr("AuthenticationType: " + dic.AuthenticationType);
pr("SchemaClassName: " + dic.SchemaClassName);
pr("Username: " + dic.Username);

COMException (0x80005000): Unknown error - UserPrincipal.set_GivenName(String value)

I have the following code which is called inside of an ASP.NET application:
public DomainUserInfo GetDomainUserInfoByName(string domain, string firstName, string lastName)
{
string[] domainArray = domain.Split(',');
foreach (string d in domainArray)
{
var principalContext = new PrincipalContext(ContextType.Domain, d);
var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName};
using (var searcher = new PrincipalSearcher(userPrincipal))
{
userPrincipal = (UserPrincipal) searcher.FindOne();
}
if (userPrincipal != null)
{
var domainUserInfo = new DomainUserInfo
{
FirstName = userPrincipal.GivenName,
LastName = userPrincipal.Surname,
Email = userPrincipal.EmailAddress,
LanID = userPrincipal.SamAccountName,
Extension = userPrincipal.VoiceTelephoneNumber,
DomainName = d,
NTAccountName = userPrincipal.Sid.Translate(typeof (NTAccount)).ToString()
};
return domainUserInfo;
}
}
return null;
}
It works when deployed on some servers but not on others, where it throws the exception:
[COMException (0x80005000): Unknown error (0x80005000)]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +386081
System.DirectoryServices.DirectoryEntry.Bind() +36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
System.DirectoryServices.PropertyValueCollection.PopulateList() +21
System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +49
System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +135
System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +288
System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +37
System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +118
System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) +34
System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() +37
System.DirectoryServices.AccountManagement.UserPrincipal.set_GivenName(String value) +17
Mfc.Inv.RM.Framework.ActiveDirectory.ActiveDirectoryManager.GetDomainUserInfoByName(String domain, String firstName, String lastName) +167
It looks like this is occurring on the line:
var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName};
when trying to set the GivenName property of the UserPrincipal object.
I'm totally stuck as to what could be causing this, especially since it works on some servers and not others. I already tried writing a console application that calls the same code it works on all of the servers, so I am guessing it has to be something to do with IIS.
here is what I am doing and if you were to hover over userFind or do a QuickWatch on it you will see the following information. also notice the IdentityType.SamAccountName that I am passing
var pc = new PrincipalContext(ContextType.Domain, domainName, null, null);
var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username);

C#/SQL Parse Error

I am trying to build a register user script using C# and SQL. However when wver I try to add the users details to the database I run into a parse error. This error is below
There was an error parsing the query. [ Token line number = 1,Token line offset = 38,Token in error = = ]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlServerCe.SqlCeException: There was an error parsing the query. [ Token line number = 1,Token line offset = 38,Token in error = = ]
Source Error:
Line 48: {
Line 49: var db = Database.Open("Database");
Line 50: var users = db.QuerySingle("SELECT * FROM Users WHERE Username = ", username);
Line 51: if (users == null)
Line 52: {
Source File: c:\Users\***\Documents\Visual Studio 2012\WebSites\CatSystem\Account\Login.cshtml Line: 50
Stack Trace:
[SqlCeException (0x80004005): There was an error parsing the query. [ Token line number = 1,Token line offset = 38,Token in error = = ]]
System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr) +136
System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan() +798
System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) +363
System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior) +59
System.Data.SqlServerCe.SqlCeCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
System.Data.Common.DbCommand.ExecuteReader() +12
WebMatrix.Data.<QueryInternal>d__0.MoveNext() +152
System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +164
WebMatrix.Data.Database.QuerySingle(String commandText, Object[] args) +103
ASP._Page_Account_Login_cshtml.Execute() in c:\Users\***\Documents\Visual Studio 2012\WebSites\CatSystem\Account\Login.cshtml:50
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +69
System.Web.WebPages.WebPage.ExecutePageHierarchy() +151
System.Web.WebPages.StartPage.RunPage() +17
System.Web.WebPages.StartPage.ExecutePageHierarchy() +62
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext context) +249
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18010
The script I am using is
#{// Initialize page
var email = "";
var username = "";
var password = "";
var confirmPassword = "";
var firstname = "";
var lastname = "";
var housenumberorname = "";
var street = "";
var city = "";
var county = "";
var postcode = "";
var tel = "";
var mobile = "";
var dob = "";
var ErrorMessage = "";
// If this is a POST request, validate and process data
if (IsPost)
{
email = Request.Form["email"];
username = Request.Form["username"];
password = Request.Form["password"];
confirmPassword = Request.Form["confirmPassword"];
firstname = Request.Form["firstname"];
lastname = Request.Form["lastname"];
housenumberorname = Request.Form["housenumberorname"];
street = Request.Form["street"];
city = Request.Form["city"];
county = Request.Form["county"];
postcode = Request.Form["postcode"];
tel = Request.Form["tel"];
mobile = Request.Form["mobile"];
dob = Request.Form["dob"];
if (username.IsEmpty() || password.IsEmpty()) {
ErrorMessage = "You must specify both email and password.";
}
if (password != confirmPassword)
{
ErrorMessage = "Password and confirmation do not match.";
}
// If all information is valid, create a new account
if (ErrorMessage=="")
{
var db = Database.Open("Database");
var user = db.QuerySingle("SELECT * FROM Users WHERE Username = ", username);
if (user == null)
{
db.Execute("INSERT INTO User (Username, Password, Firstname, Lastname, House, Street, City, County, Postscode, Tel, Mobile, Email, Dob) VALUES (#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12)", username, password, firstname, lastname, housenumberorname, street, city, county, postcode, tel, mobile, email, dob);
WebSecurity.CreateAccount(username, password, false);
// Navigate back to the homepage and exit
Response.Redirect("~/");
}
else
{
ErrorMessage = "Email address is already in use.";
}
}
}
}
#if (ErrorMessage!="")
{
<p>#ErrorMessage</p>
<p>Please correct the errors and try again.</p>
}
I assume there is something wrong with the SQL command but as I am unfamiliar with MS SQL I can not see the issue. Any help with this would be appreciated.
The SQL is not valid. If Username is a VARCHAR or CHAR type, you need to enclose the value in ', though a better option is to use a parameterized query, as using string concatenation/formatting means your application is open to SQL Injection .
var users = db.QuerySingle(
string.Format("SELECT * FROM Users WHERE Username = '{0}'",
username));
You should change your code to use the parametrization as you have in your insert:
var user = db.QuerySingle("SELECT * FROM Users WHERE Username = #0", username);
Currently your query does not contain the username from username it is just
SELECT * FROM Users WHERE Username =
Which is invalid sytax.
From : http://wekeroad.com/2011/01/13/someone-hit-their-head
var db = Database.Open("TDL");
var selectQueryString = "SELECT * FROM Articles WHERE slug = #0";
show = db.QuerySingle(selectQueryString, slug);

Categories

Resources