Thursday, May 20, 2021

Buggish: C# Replacing \n with
versus \r\n versus \n\n

Problem to be solved

I need to replace MS SQL break lines with HTML break lines so that it can be displayed on a web page.

Background

Our network group decided to make routers reject posts that are too big. I have a tool to compare database objects across multiple environments to validate and to promote. I used a text box to display the sql queries. If there were five environments, then there would be five textboxes. If the stored procedure was really large, then I would have it five times. So when I post back to the server, I could potentially return a lot of data.

I don't want to truncate because most new changes are at the bottom. I don't want to skim because are at the top. And fixes are typically in the middle.

So decided to use html <pre> tag. This requires me to convert new lines to html new lines. I'm usually lazy and just convert \n to <br/> but then I realized that this returned two break lines which caused a blank line in between every line. This was a pain because that means it took me twice as long to skim through the code.

For some odd reason, I felt like doing both a \n\n and \r\n replace and found this did the same... even though the string value was just \r\n. This is really odd. Although works for me, this is not what I expected.


Final decision

string.Replace("\r\n", "<br/>");

Friday, May 14, 2021

Anecdote: Replacing plaintext user id and password with password vault

I just reviewed code where they are trying to replace configuration files that have user ids and passwords in plaintext to a password vault. The hilarious part is that they comment out the old configuration file which contains the credentials.

The habit here is that developers are always afraid to lose code even if there exists a million backups (or maybe because their exists a million backups), so they comment out old sections.

The code even goes through two code reviews before it reaches me. 

#GiveMeTheirPaychecks

Monday, May 10, 2021

Buggish: Unable to access MS Network Folder (IP versus HostName)

Problem (rare)

I am able to some times get to the machine's base share folders but unable to access sub-folders. I have share permissions and local machine permissions on the network machine. This seemed random because I was able to connect to other shares with IP address and hostnames. I have a different login to access the network folders than the system that I am working from.

Solution (workaround)

Be consistent with the address to the network folder. If logged in with IP address (\\10.10.10.10), continue to use IP address. If logged in with hostname (\\hostname), continue to use hostname. If you logged in with IP, then you may get access issue when using hostname. Also the same vice-versa. I was able to replicate this every time.

(Edit: 6/12/2021) Consistency is key. Using simple hostname or the fully qualified hostname (e.g. \\hostname vs \\hostname.domain.local) will also have the same issue. The trickier part to this is that net use will see these are the same and will not allow the user to provide login for both \\hostname and \\hostname.domain.local. This makes it very hard to switch. I had an post on how to remote net use but it does not work. My only solution is to reboot the system each time this needs to be corrected.

Background

We moved from one cloud platform to another. Before the group used IP addresses to connect to network folders. After the migration, we started to use hostnames. The infrastructure team also created a tunnel between the two cloud platforms. The tunnel was only accessible with IP.

When on the new system, I was still switching between IP and hostname depending on where I was copying the server address from. So I will oftentimes switch between the two depending which was readily available for me.

I do not think many people will face this problem as most people will be consistent with the address. The last oddity was that I can ping the hostname and resolves to the same IP but for some reason the destination folder does not recognize that as the same login. (I mean, I can probably guess why this is happening from the problem but not sure why it was designed this way.)


Reference

https://douglastclee.blogspot.com/2021/10/buggish-unable-to-access-ms-network.html (added 2021-10-06)

Thursday, May 6, 2021

Buggish: MS Error Copying Files to Network Share after granting permissions without reboot

I hate rebooting, so I try my best to look for solutions even if it costs me more time. In this case, I am trying to copy a file to another new network share. I get an error that I do not have permissions to do so. Then I tried to use "net use" to disconnect and reconnect with the same error.

Solution

Close all Windows Explorer windows.
  • Closing one did not resolve my issue
  • Restarting the Windows Explorer may also resolve this issue. This will likely close all Window Explorer windows anyways.
  • net use /delete does not work
    • The network share wasn't even on my net use list


Potentially Solves Another Issue

Sometimes "net use" does not allow me to add another network share with different credentials saying that it cannot manage multiple shares. I'll use "net use /delete" to remove the connection. Then when I try to add it again, it still gives me the same error. Even after I "net use" again to check, the connection is no longer there. When I try to delete it again, it says it does not exist.

I believe this may be held up by the windows explorer that I keep open.



Reference

https://stackoverflow.com/questions/24933661/multiple-connections-to-a-server-or-shared-resource-by-the-same-user-using-more

Wednesday, May 5, 2021

Info: Find my system's network folder access' user (wmic, gwmi)

 I am wrote a tool to move objects from one server to another. To do this, I use network folders. This is a web tool so I created a local user on each machine that I need access to then created a share folder giving access to my local user.

Problem

While troubleshooting on my development system, my current login user sometimes is used to access the network folder instead of the userid that I wish to use. This occurs sometimes if I use Windows Explorer to check for files before I launch my tool. If my domain account has access to the network folder, this sets my current user account to the network folder. If it does not, then none is set. This was confusing because I was able to access certain folders but not others.


My Solution

I needed a way to see what user account is used to access the network folders. The following are a couple commands that I found that were useful to me.

1. wmic netuse get remotename,username

2. gwmi -Query 'Select LocalName, RemoteName, UserName from Win32_NetworkConnection'


#2 requires powershell to execute


Additional Info

Here are a couple other commands from the page that I didn't need but may be useful to others or my future self.

1. wmic netuse where LocalName="Z:" get UserName /value

2. gwmi -Query 'Select * from Win32_NetworkConnection' | Select-Object LocalName, RemoteName, UserName, ConnectionState | Sort-Object LocalName | ft -auto

3. rundll32.exe keymgr.dll, KRShowKeyMgr



Reference

https://stackoverflow.com/questions/9037503/determine-domain-and-username-used-to-map-a-network-drive