Get AD Users Visual Studio Web App versus Console - asp.net-core

I have the following code below.
When I run Debug in Visual Studio with this code in an ASP.NET Core App (so running as IIS Express) this works
When I run Debug in Visual Studio with this code in a ASP.NET hosted process in a Windows Service this return nothing, but also no error messages
I connect from my home laptop via RDP to another laptop where VPN is running, so I think that is probably it. I tried running visual studio as admin, running the compiled exe as admin, /runas with the domain specified, etc but the commandline app will show nothing while the asp.net core app shows the list. So it must be the user it runs under.
But when i run WindowsIdentity.GetCurrent().Name in both cases it gives the same domain and name (me). In task manager the devenv and iis process is me.
public List<AdSecurityGroupDTO> GetAllDomainSecurityGroups(string domain)
{
List<AdSecurityGroupDTO> result = new List<AdSecurityGroupDTO>();
using (var ctx = new PrincipalContext(ContextType.Domain, domain))
{
GroupPrincipal findAllGroups = new GroupPrincipal(ctx, " * ");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach (Principal group in ps.FindAll())
{
AdSecurityGroupDTO adSecurityGroupDTO = new();
adSecurityGroupDTO.Name = group.Name;
adSecurityGroupDTO.Description = group.Description;
adSecurityGroupDTO.DisplayName = group.DisplayName;
adSecurityGroupDTO.DistinguishedName = group.DistinguishedName;
adSecurityGroupDTO.SamAccountName = group.SamAccountName;
result.Add(adSecurityGroupDTO);
}
return result;
}
}

Related

Puppeteer sharp in Blazor page Failed to create connection

I created a project in blazor using Visual Studio 2022 and dot net 6.0.
He added the PuppeteerSharp package and created a small test function:
try
{
using (var myBr = new BrowserFetcher())
{
await myBr.DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
var launchOptions = new LaunchOptions { Headless = true };
_browserPuppeter = await Puppeteer.LaunchAsync(launchOptions);
_pagePuppeteer = await _browserPuppeter.NewPageAsync();
await _pagePuppeteer.GoToAsync(url);
}
}
catch (Exception exc)
{
errore = exc.Message + DateTime.Now.ToString(" HH:MM:ss.zzz");
}
When I run it from visual studio locally it loads the page correctly. When I publish on a website in Web Hosting (https://www.webwiz.net) two things happen:
fails to download chromium.
If I upload the directory .local-chromium via ftp, it gives me the error:
"Failed to create connection"
Any idea?

WebSockets not working when application is built

I have got to ASP.NET-Core 2.0 apps communicating via WebSockets.
App A is Server.
Application A is running on a remote server with Ubuntu.
App B is Client
Application B is running on a PC setup in my office.
When I test my applications locally in Debug everything works fine. Client connects to the server and they can exchange information.
However, when I build my Server app, Client can connect to it but when server tries to send a message to the client the message is not received by the client.
public async Task<RecievedResult> RecieveAsync(CancellationToken cancellationToken)
{
RecievedResult endResult;
var buffer = new byte[Connection.ReceiveChunkSize];
WebSocketReceiveResult result;
MemoryStream memoryStream = new MemoryStream();
do
{
if (cancellationToken.IsCancellationRequested)
{
throw new TaskCanceledException();
}
Console.WriteLine("Server Invoke");
// result never finishes when application is build. On debug it finishes and method returns the correct result
result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
if (result.MessageType == WebSocketMessageType.Close)
{
await CloseAsync(cancellationToken);
endResult = new RecievedResult(null, true);
return endResult;
}
memoryStream.Write(buffer, 0, result.Count);
} while (!result.EndOfMessage);
endResult = new RecievedResult(memoryStream, false);
return endResult;
}
This is the part of code where everything hangs.
What I tried was:
Build Server - Build Client => not working
Build Server - Debug Client => not working
Debug Server - Debug Client => working
I need any advice what might be wrong here and where I should look for issues.
Console if free of errors. Everything hangs on:
result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);

Servicestack, Xamarin and authentication

I've got an ServiceStack service running with custom authentication, this runs fine from the browser and through a Windows console program.
I'm now trying to get a simple Xamarin Android program to authenticate but whatever I try it crashes with an Exception without any further explanation. The code I am using stops at the line with 'var authResponse', I'm using the 4.0.44 ServiceStack packages and the lastest stable Xamarin from inside VS2015.
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// servicestack
var client = new JsonServiceClient("http://10.0.2.2:8080");
var authResponse = client.Get<AuthenticateResponse>( new Authenticate
{
UserName = "Willem",
Password = "secret",
RememberMe = true
});
Any pointers to what/where I should look?
tia
If this is a self-hosted Service you would need to register the HttpListener AppHost to accept requests from different hosts by listening on a host wildcard, e.g:
appHost.Start("http://*:8080/");

HTTP could not register URL (remote debugging)

C#, Windows 7.
I write an AutoCAD plugin and use the remote debuging (MS Visual Studio). My plugin must work as a WCF service. AutoCAD is unmanaged application and must to be as a host for my service. I am reading a book about WCF, and I try use it. I can't use acad.exe.config for my service settings: I have not permission. So I do it myself (I will read them from my xml file, but later, after refactoring). Code of my "server" (this code start by AutoCAD):
private static void RunServices() {
Ap.Document doc = cad.DocumentManager.MdiActiveDocument;
try {
Uri address = new Uri("http://localhost:8000/CadService");
BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "httpBinding";
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.Security.Mode = BasicHttpSecurityMode.None;
host = new ServiceHost(typeof(CadService));
host.AddServiceEndpoint(typeof(ICadService), binding, address);
host.Open(); // I get an Exception here...
if (doc != null) {
doc.Editor.WriteMessage("Service launched.\n");
}
}
catch (Exception ex) {
if (doc != null) {
doc.Editor.WriteMessage("Exception: {0}\n", ex.Message);
}
}
}
I get an exception (look the code comment):
Exception: HTTP could not register URL http://+:8000/CadServices/.
Your process does not have access rights to this namespace
(see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
But the http://go.microsoft.com/fwlink/?LinkId=70353 page is not exist. I try launch MS Visual Studio 2013 as admin (I read about this here), but It is not help me (look P.S.2 bellow).
P.S. If I launch AutoCAD as admin - all works fine.
P.S.2 If I launch the remote debugger as admin - all works fine too.
But I need use it as a usual user. Can I start my service (hosted in the AutoCAD) without the admin rights?
This is probably because AutoCad does not have the required rights to register the port in HTTP.SYS. In that case you have two options:
Start Autocad in Admin mode
Register the port / endpoint in HTTP.SYS manually. For this, there are several tools available. This is the one I would use : http://www.codeproject.com/Articles/437733/Demystify-http-sys-with-HttpSysManager
Let me know if this works

Deploying a WCF service on a remote pc

I have a WCF web service, which I have hosted in IIS on my PC. This I have added as Service Reference in a Visual Studio Project and I am able to invoke it successfully in a web service in this VS Project.
Now I am trying to deploy this web services on a remote PC, which doesn't have Visual Studio installed. For this purpose I copied the .svc and web.config files and bin folder of this web services to a folder on the remote PC. Then I hosted the services on IIS pointing to the respective folder. Now when I browse the web service using the .svc link I am able to access the web service from my PC. I added this web service as a service reference in my Visual Studio project and everything looks OK, I am able to see the separate methods and their parameters in the Object browser. The problem appears when I try to invoke methods from this web services.
I am able to invoke one method and then when I invoke the second one I get the following error (The absolutely same web service and all its methods work perfectly if hosted in IIS on my PC.):
Object reference not set to an instance of an object.
Here is a part of the method where I invoke the web service (the web service name is TFSWS):
public void ImportRequirements(string username, string password)
{
TFSWS.TFSWSClient obj = new TFSWS.TFSWSClient();
string projects = obj.GetTFSProjects(username, password, TFS_URI);
string list = obj.GetAllWorkItems(ProjectName2, username, password, TFS_URI, WItypes);
Here is the code of the first method which I am able to invoke successfully from TFSWS:
public string GetTFSProjects(string userName, string password, string Uri)
{
StringWriter MyStringWriter = new StringWriter();
NetworkCredential cred = new NetworkCredential(userName, password);
TfsTeamProjectCollection _tfs = new TfsTeamProjectCollection(new Uri(Uri), cred);
_tfs.Authenticate();
ICommonStructureService tfsProjectService = (ICommonStructureService)_tfs.GetService(typeof(ICommonStructureService));
ProjectInfo[] projects = tfsProjectService.ListAllProjects();
string[] proj = new string[projects.Length];
for (int i = 0; i < projects.Length; i++)
{
proj[i] = projects[i].ToString();
}
DataTable ProjectsDT = GetDataTableFromArray(proj);
...
}
Here is the code of the second method that I invoke from TFSWS and that throws the error message (When I debug I can see that all parameters are assigned correctly):
public string GetAllWorkItems(string projectName, string username, string password, string URI, string[] WItypes)
{
StringWriter MyStringWriter = new StringWriter();
NetworkCredential cred = new NetworkCredential(username, password);
TfsTeamProjectCollection _tfs = new TfsTeamProjectCollection(new Uri(URI), cred);
_tfs.Authenticate();
WorkItemStore _witStore =(WorkItemStore)_tfs.GetService(typeof(WorkItemStore));
DataTable myData = new DataTable();
string project = projectName;
string[] m_columns;
Hashtable context = new Hashtable();
Project proj = _witStore.Projects[project];
}
string myQuery = "SELECT [System.Id], [System.Title], [System.WorkItemType], [System.State] FROM WorkItems WHERE ...";
WorkItemCollection result = _witStore.Query(myQuery, context);
DisplayFieldList fieldList = result.Query.DisplayFieldList;
...
}
You can debug remotely to figure out the issue the code is experiencing on the target machine, without installing all of Visual Studio on it. You just need to deploy debug instead of release mode assemblies, and have the remote debugger service running on it.
Remote Debugging Setup