Windows 8 Wireless Network Management
With Windows 8 you can no longer delete wireless networks that are not in range. You can drop down and do this from the command line – but for the novice user this may not be easy. I created a small little .Net 4.0 WPF app that will wrap the shell commands and allow you to list all networks and delete them.
Here is the source Article that inspired the idea. http://lifehacker.com/delete-wi-fi-networks-in-windows-8-from-the-command-lin-513411677
Command used to show profiles:
netsh wlan show profiles
Command used to delete a profile:
netsh wlan delete profile name=[profile name]
You can download the utility from the following location:
http://tsells.com.jasmine.arvixe.com/host/tools/WirelessNetworkRemovalTool.zip
You can download the source code from here:
http://tsells.com.jasmine.arvixe.com/host/tools/WirelessNetworkRemovalTool_Source.zip
How to block Microsoft Office Applications from connecting to Microsoft SQL Server
A security loop hole exists in Microsoft SQL Server when using windows authentication. This occurs when a user who has privileges to access a database but is limited in what data can be seen via Application rules can use Excel or Access to connect and see all data. The following work around can block access to the server from any Microsoft Office Application.
Caveat:
This blocks access for ALL databases on the server.
Run the following script against the server with admin privileges and the applications will now be blocked.
USE master; GOCreateTRIGGER OfficeApplicationBlock ONALL SERVER FOR LOGON ASBEGINIFAPP_NAME() LIKE'%Microsoft Office%'ROLLBACKENDGO
If the application name has changed the modify the script to use the new application name.
Note this can be used to block any application based on name. You can use SQL Profiler to see the names of the applications when they are connecting.
How to setup an “always on” SSH tunnel between Mac and Home Network (Windows) for SVN
Now that I am developing iOS apps I am using my existing home network setup that includes source control servers to host my SVN source control. I prefer to do it this way since I already have the hardware, the servers, and the backup solutions in place to the cloud. There is no reason to purchase a SVN hosting contract.
By setting it up this way you are also securing your transmission between your machine and prying eyes (wireless network sniffing). This is a great solution for coffee shops. ![]()
Step 1 – Setup your SVN Server
I prefer to use VisualSVNServer from http://www.visualsvn.com/. I have used it in the past and it works well for me.
- Download the software
- Install the server and configure
- Ensure you can access the server from inside your home network
- Step 2 – Setup your SSH Server / Tunnel
- I prefer WinSSHD from Bitvise. http://www.bitvise.com/winsshd. It is free for personal use.
- Download the software
- Install the server and configure
- Be sure to open a port on your firewall (22 is the default SSH port) to point to your internal machine / server
- Ensure you have a way to get back to your home network (most IP addresses are not static – and while they may not usually change – if it does you will not be able to connect. I use DynDNS http://dyn.com/dns/ for this.
- On your MAC – download and install SSH Tunnel Manager from the Mac Store
- Add a connection in your preferences to point to your home server via the dynamic url created from the service of your choice. Put your username you setup to connect with in the login field. Under the local redirections add a local port (example 30081) that points to the internal IP of the server / machine hosting the SVN server. Be sure to put the correct internal port as well. I use port 81 since IIS is using port 80.
- Under options check Auto Connect and Handle Authentication.
- Also change the crypt method to something more secure – like aes256xxx
- Now test connection. It will prompt you to enter a password. It should stay connected. Note you may have to launch SSH Tunnel after a reboot.
Step 3 – Connect to your SVN Repository in Xcode
- Launch Xcode
- Add another repository entry for SVN using the connection information from above. Remember this will be your local address. Example http://localhost:30081/svn/rep.
- Troubleshooting
One trick in testing all this is to have two links setup in your web browser. One pointing to the SVN repository from inside the network – and one using the SSH tunnel connection. Once both of these links are working – you can then move into Xcode and troubleshoot from there.
How to connect to an SSH Tunnel on OSX
I am setting up source control for my new iOS development endeavors. I already have a TFS server in place and backups set up – but wanted to add SubVersion to it since I already had the hardware. I currently can connect to my TFS server from anywhere using an SSH tunnel. I want to do the same for Subversion so here is how I did it.
1. Install an SSH Server (I used the server from Bitvise.com – it’s free for personal use).
2. On your Mac – create a new text file using a text editor (I was using Text Wrangler).
3. Enter the following in your text file
- 40443 – local port number
- 192.168.1.100 – local ip address of machine inside SSH Tunnel Server’s network (used for remapping)
- 443: – port to connect to on internal server address
- -l loginname : enter username for connection
- mysshserverlocation.com – IP or DNS name of your SSH Server
- 4. Save the file as a .sh (ex: myfile.sh)
- 5. Open a terminal window – navigate to the location of the file.
6. Run the following command to make the file executable chmod +x myfile.sh
7. Right click on the file and select open with – select other
8. Select All Applications under Enable – then select terminal – and check the box always open with
9. Now just double click on the file – enter your password – and your SSH Tunnel is now working on your MAC.
How to Test Email Messages in your application
I have seen many questions on stackoverflow about how to test the sending of an email message. I figured it was time to provide what I feel is a good example on how to test your system. A couple of things need to be stated first.
1. For a Unit Test – you should not have any dependencies on external systems (File System, Database Server, SMTP Server, etc.).
2. For an Integration Test – you can have these dependencies. For some scenarios – Integration Tests provide more value than unit tests and vice versa.
This example does not take dependencies on the SMTP Server. I am providing what some would consider a “mock” for this. I am usually against mocking as it usually proves that the costs far outweigh the benefits. Note this is my opinion – yours may differ.
For this example I have set up two projects. An email services class library and a test project (MSTest).
For this demo we have a few things we want to achieve.
- We want to confirm an email can be composed and sent via our services class
- We want to verify the body of our message is valid
So how can I create an email “manager” that I can verify sends emails under a test environment without an SMTP server and at the same time use inside an application to really send emails? The answer should be obvious – I abstract the sending of an email via an interface and an override function.
My Email Sender Interface
namespace EmailTest.Services { public interface IEmailSender { System.Net.Mail.MailMessage Message { get; set; } bool SendEmail(System.Net.Mail.MailMessage message); } }
Implementation of Production Email Sender. This will actually send an email when used by your application (after the specifics are implemented).
internal class EmailSender : IEmailSender { public MailMessage Message { get; set; } public EmailSender() { GetSettings(); } public bool SendEmail(MailMessage message) { Message = message; // Send message using SMTPClient here return true; } private void GetSettings() { // Load SMTP server settings here } }
Implementation of Mock Email Sender
public class EmailMockSender : IEmailSender { public MailMessage Message { get; set; } public bool SendEmail(MailMessage message) { Message = message; return true; } }
EmailComposer - This is a crude example – but will usually be the heart of your email “engine”.
public class EmailComposer { public static MailMessage BuildMessage() { var message = new MailMessage("testuser@somedomain.com", "testuser@somedomain.com"); message.Subject = "My Test Message"; message.Body = "My body of my messsage"; return message; } }
Email Manager – this is the class that coordinates the sending of your emails. Again this is the most basic of examples and does not implement any type of queuing or retry strategy.
public class EmailManager { private IEmailSender _sender; public EmailManager() { _sender = new EmailSender(); } public void OverrideEmailSender(IEmailSender sender) { _sender = sender; } public bool SendMessage(MailMessage message) { return _sender.SendEmail(message); } public MailMessage GetMessage() { return _sender.Message; } }
Notice here that we have a public method to override the sender. This can be done in a more elegant manner – but for the purposes of this demo – this is sufficient.
Now we can focus on our Test Class. This is the class we are going to use to verify the 2 basic functions highlighted earlier.
[TestClass] public class EmailTest { [TestMethod, Description("Verify Message can be sent")] public void TestEmailCanBeBuiltandSent() { MailMessage message = EmailComposer.BuildMessage(); var manager = new EmailManager(); manager.OverrideEmailSender(new EmailMockSender()); bool sentMessage = manager.SendMessage(message); Assert.IsTrue(sentMessage); } [TestMethod, Description("Verify Message can be sent")] public void VerifyBodyofEmailMessage() { MailMessage message = EmailComposer.BuildMessage(); var manager = new EmailManager(); manager.OverrideEmailSender(new EmailMockSender()); manager.SendMessage(message); string bodyOfMessage = manager.GetMessage().Body; Assert.IsFalse(string.IsNullOrEmpty(bodyOfMessage)); } }
Remember – the ultimate goal of testing our email “engine / class / manager / etc.” is to verify the basic functionality is working. We are not trying to confirm the SMTP Server is up and sending emails – we can leave the bulk of that work for integration tests and the vendor who wrote the SMTP server software.
Source Code – http://tsells.com.jasmine.arvixe.com/host/code/EmailTestDemo.zip
How to maintain UTC date times across time zones and account for Daylight Savings Time
When creating an application that needs to store dates with time data that must be stored and displayed consistently for all users across multiple time zones / countries, etc. the recommendation is to store in UTC format and use the DateTimeOffset field in SqlServer. However there is an issue with this as it stores the dates correctly – until DST kicks in and previously stored data now is off by an hour.
Example:
Here is the output of inserting and retrieving a date in / from the database.
It was stored in the database as UTC.
This was run at 9:10 pm.
Local date: 10/25/2012 9:10:34 PM
10/25/2012 10:10:34 PM -04:00
I changed the date to November 30th. You see where the data is still the same – but is now displayed incorrectly (local date)?
Local date: 10/25/2012 10:10:34 PM
10/25/2012 10:10:34 PM -04:00
The second run also inserted a new value – and it looks correct – but the offset is different
Local date: 11/30/2012 9:12:24 PM
11/30/2012 9:12:24 PM -05:00
Now I change the date back to October 25th
The first run is correct again
Local date: 10/25/2012 9:10:34 PM
10/25/2012 10:10:34 PM -04:00
The second run is an hour early
Local date: 11/30/2012 8:12:24 PM
11/30/2012 9:12:24 PM -05:00
The 3rd run is correct
Local date: 10/25/2012 9:13:57 PM
10/25/2012 10:13:57 PM -04:00
Here was the code being used to calculate the date to insert for these runs. Notice I am accounting for the Daylight Savings Time.
var date = DateTimeOffset.Now; var newdate = date; if(TimeZoneInfo.Local.IsDaylightSavingTime(date)) { newdate = date.AddHours(1); }
So the question is how do I stored the date, have it always work, and not have to add an extra field to my database to track if I was in DST mode or not?
// Get Current local date var date = DateTime.Now; // Get current local offset var offsetspan = TimeZoneInfo.Local.GetUtcOffset(date); // Check to see if is DST if (TimeZoneInfo.Local.IsDaylightSavingTime(date)) { // if DST - add one hour to the offset offsetspan.Add(new TimeSpan(1, 0, 0)); } // Get your new date using the offset var newdate = new DateTimeOffset(date, offsetspan);
Now you insert that date into your database. When pulling out – use the following code to convert back to local time.
var newdate1 = Convert.ToDateTime((dataRow[0].ToString())); var offset = new DateTimeOffset(newdate1); var localdate = offset.LocalDateTime;
Now when you do multiple runs with different DST settings – you always get the correct value back. If you notice the only thing different is the offset that was stored.
November 30th Run
Local date: 11/30/2012 8:52:28 PM
11/30/2012 8:52:28 PM -05:00
October 26th run
Local date: 11/30/2012 8:52:28 PM
11/30/2012 8:52:28 PM -05:00
Local date: 10/25/2012 8:53:10 PM
10/25/2012 8:53:10 PM -04:00
November 30th run
Local date: 11/30/2012 8:52:28 PM
11/30/2012 8:52:28 PM -05:00
Local date: 10/25/2012 8:53:10 PM
10/25/2012 8:53:10 PM -04:00
Local date: 11/30/2012 8:53:52 PM
11/30/2012 8:53:52 PM -05:00
Use Linq to flatten nested lists into a single list
Linq has an awesome feature called SelectMany() that will flatten out nested lists for you so you can iterate over them in one pass without having to do nested foreach loops.
Iterating through the nested lists using nested foreach loops.
public static void ForEachListing() { // Build a dictionary of nested strings var mydictionary = new Dictionary<int, List<string>>(); for (int i = 0; i < 2; i++) { var myStrings = new List<string>(); for (int j = 0; j < 2; j++) { myStrings.Add(string.Format("{0}_MyString_{1}", i, j)); } mydictionary.Add(i, myStrings); } foreach (KeyValuePair<int, List<string>> keyValuePair in mydictionary) { foreach (string s in keyValuePair.Value) { Console.WriteLine(s); } } }
Iterating through the nested lists using Linq.
public static void LinqListing() { // Build a dictionary of nested strings var mydictionary = new Dictionary<int, List<string>>(); for (int i = 0; i < 2; i++) { var myStrings = new List<string>(); for (int j = 0; j < 2; j++) { myStrings.Add(string.Format("{0}_MyString_{1}", i, j)); } mydictionary.Add(i, myStrings); } foreach (string s in mydictionary.SelectMany(x => x.Value).ToList()) { Console.WriteLine(s); } }
Output – both are the same.
Caveats: When this does not work / work well.
When you need access to the original key from the dictionary inside your loop. For example if you wanted to print out the corresponding dictionary key for every string in the list – the SelectMany() option will not work.