# Saturday, 29 October 2011

In a post about two months back I worked with Adam Anderly to update the SimpleMembershipMvc3.Sample application to include resetting/forgotten passwords and emailing verification of registration. 

Adam updated his NuGet package to include this.  He also separated the email code into a service to make it better for testing.  Go Adam!

How to get it

If you are familiar with NuGet the command is:

Install-Package SimpleMembership.Mvc3.Sample

If you aren’t familiar with NuGet yet, you need to be.  It’s a great tool!

You can also get the code via GitHub.

Happy Coding!

Posted 10.29.2011  #    Comments [0]  | 
# Wednesday, 29 September 2010

Our team is working on a UI facelift for an intranet application that helps drive our internal yearly budgetary process.  During the beginning stages of the development of this effort we hired a new webmaster and we invited him to the effort to help give our UI a nice finish with all the CSS goodness and jQuery goodies.

During this process our webmaster began using some CSS3 magic to make things look nice.  As most good webmasters and designers firefox was used as the start of the design.  For our intranet however, IE is king and we have a mix of IE7 - 8 and they don’t support many CSS3 features.  Our webmaster turned to CSS3Pie.  I had no idea about this little gem and he had just recently come across it.  Download it, write your CSS3 and apply PIE and magically Internet Explorer renders CSS3.

As an example of what it can do, what if you wanted to apply a nice rounded edge to an element.  Fire up VS 2010 and start a new web forms project.  Let’s round the edges of the bottom of the border in the default page.

image

Let’s edit the page element in the site.css to get this going.

.page
{
    width: 960px;
    background-color: #fff;
    margin: 20px auto 0px auto;
    border: 1px solid #496077;
    border-radius: 0px 0px 10px 10px;
    -moz-border-radius: 0px 0px 10px 10px;
    -webkit-border-radius:  0px 0px 10px 10px;
}

We’ve added three items. The W3C standard for adding a rounded border is border-radius but for backwards compatibility we add the –moz-border-radius for older versions of Firefox and –webkit-border-radius for older versions of Chrome.

Now fire these up in Firefox and Chrome and you will have your rounded corner.  If you are using IE however, no luck.

Firefox sample:

image

Now to make this magic happen in IE we sprinkle in just a dash of CSS3Pie.  After downloading CSS3 Pie add the .htc file to your project and then add the below line to your page element in the site.css file:

.page
{
    width: 960px;
    background-color: #fff;
    margin: 20px auto 0px auto;
    border: 1px solid #496077;
    border-radius: 0px 0px 10px 10px;
    -moz-border-radius: 0px 0px 10px 10px;
    -webkit-border-radius:  0px 0px 10px 10px;
    behavior: url('./Scripts/PIE.htc');
}

Fire up IE again and CSS3 rounded corners appear thanks to CSS3Pie.

image 

Happy Coding

Posted 09.29.2010  #    Comments [0]  | 
# Friday, 07 May 2010

A few posts back I wrote about adding character counting to the .Net AJAX HTML Editor.  Recently, I received a question asking how to hide and show the character count element for the .Net AJAX HTML Editor when the editor goes in and out of focus.  The short and sweet answer is to wire up a function on the focus and blur events of the editor control and call the hide / show methods in jQuery on your count element.

$(GetEditor()).focus(function() {
  $(Editor1InfoArea).show();
}).blur(function() {
  $(Editor1InfoArea).hide();
});

Just add the above code in the $(document).ready function and away you go.  The GetEditor function and Editor1InfoArea variable are referenced in code in the original post.

However, jQuery has a lot to offer and I wanted to show some of these offerings.  I took advantage of method chaining to save myself having to call $(GetEditor()) twice as shown below.

$(GetEditor()).focus(function() { $(Editor1InfoArea).show();  });
$(GetEditor()).blur(function() { $(Editor1InfoArea).hide(); });

If your new to jQuery chaining may throw you a bit but it works great for saving keystrokes and once you get used to the syntax can be easy to follow. 

Beyond the show and hide methods you could call on many other methods to add some panache (Come on, give this one to me,  I am French after all).

You could replace show/hide with some of these combinations:

Nothing like making your app a little more appealing with some animation.  But wait! There’s more.

You can also change the style with the css method:

$(Editor1InfoArea).css("display", "block");
$(Editor1InfoArea).css("display", "none");
Or
$(Editor1InfoArea).css("visibility", "");
$(Editor1InfoArea).css("visibility", "hidden");

Of course, you are using classes to make easier management of your styling right?  You can use the addClass/removeClass methods:

$(Editor1InfoArea).addClass("showCounter");
$(Editor1InfoArea).removeClass("showCounter");

* What about the toggle method? Ah, I’m glad you asked. In this case since we are tracking the focus and blur events you may end up with the toggle working the opposite as you expect.  This is because toggle does the opposite of what the targeted element’s visible value is when called.  You can fix this by calling the show or hide on your element after the focus/bind methods are setup.  Toggle is really designed for the click event but can be used in this situation.

So there you have it.  More ways than you probably care to know and I wouldn’t be surprised if there were more. 

A parting thought.  Earlier this week I read a great post on the performance of show/hide vs. toggle, css and addClass/removeClass.  Check it out if you want to read more on it but the basic answer is calling the css method is the best.  However, there is one more way that’s even faster but you’ll just have to read the article to find out what it is.

Happy Coding! :-)

Posted 05.07.2010  #    Comments [0]  | 
# Saturday, 27 March 2010

I need to take a CSV file of logins with their emails and verify if these logins exist in our Active Directory.  This can be done several different ways including with powershell but for one reason or another I want/need to do it with .Net.

I will be using an open source library called FileHelpers to make the parsing of the CSV file a snap.  I’m going to skip over the code that uses FileHelpers and focus on the AD lookup.  Check out my previous post on FileHelpers to see how easy FileHelpers makes parsing.

I’m going to use the System.DirectoryServices namespace to lookup AD information.

Let’s look at the AD Lookup class I created to compare the login in the CSV file to AD.

using System;
using System.Collections.Generic;
using System.DirectoryServices;

namespace VerifyUsers
{
    public class ADLookup
    {
        private DirectorySearcher _directoryToSearch;

        public ADLookup()
        {
            DirectoryEntry root = new DirectoryEntry("LDAP://rootDSE");
            string rootName = "LDAP://" + root.Properties["defaultNamingContext"].Value.ToString();
            DirectoryEntry searchRoot = new DirectoryEntry(rootName);
            _directoryToSearch = new DirectorySearcher(searchRoot);
            _directoryToSearch.PageSize = 100;
            _directoryToSearch.SearchScope = SearchScope.Subtree;
            _directoryToSearch.PropertiesToLoad.Add("displayName");
        }

        public bool IsUserInAD(string login)
        {
            _directoryToSearch.Filter = string.Format("(&(sAMAccountName={0})(objectClass=Person))", login);
            SearchResultCollection personFound = _directoryToSearch.FindAll();

            return personFound.Count > 0;
        }
    }
}

I’m using the DirectorySearcher class to look in AD for user information.  In the class constructor I setup the connection information to the LDAP store and set some of the basic properties.

The SearchScope property is important to set to what you need.  I set it to Subtree (which is the default) to have search include the base and it’s children.  If for example, your AD environment has subfolders then you want this set in order to search the subfolders.

In the IsUserInAD method call I setup the query to look for the login name better known as the sAMAccountName in LDAP and I only want it to search Person objects.

Note – If you are creating your own project you will need to add a reference to System.DirectoryServices.

Now let’s get the code running in a console:

using System;
using System.Collections.Generic;

namespace VerifyUsers
{
    class Program
    {
        static void Main(string[] args)
        {
            Parser userParser = new Parser("Users.csv");
            DisplayUsersNotInAD(userParser.Parse());

            Console.ReadLine();
        }

        static void DisplayUsersNotInAD(User[] list)
        {
            ADLookup lookup = new ADLookup();
            foreach (User u in list)
                if (lookup.IsUserInAD(u.login) == false)
                    Console.WriteLine("{0} was not found in AD.", u.login);
        }
    }
}

First, we parse the CSV file using the FileHelpers library.  Then we iterate over the list and display the entries not found in our directory.

The CSV file I am using has three entries and the first two are bogus accounts that don’t exist in AD.  The third entry does.  If the code works correctly we should only have the first two entries reported as not found.

When you run the code you should see:

image

The last entry, kdog, was found in AD.  We can do whatever we need to with the two entries that aren’t in AD.

Download the C# source or VB Source.

Read my previous post on using the open source FileHelpers library to parse CSV files.

Happy Coding!

Posted 03.27.2010  #    Comments [0]  | 
# Saturday, 20 March 2010

Need to parse a text file or perhaps a stream?  Tired of writing parsing code to do this?  Well, for only $19.95….  Ha, ha,  just kidding.  I ran across FileHelpers earlier this week and was impressed.

FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.
The idea is pretty simple:

You can strong type your flat file (fixed or delimited) simply describing a class that maps to each record and later read/write your file as an strong typed .NET array

The Library also has support for import/export data from differents storages like Excel, Access, SqlServer, etc. – Home page of FileHelpers.com

Setting it up and getting started is very easy.  I’ll walk you through a sample.

I have a CSV file with usernames and email addresses in it.  I need to parse that file and I need to verify the information in the CSV against another data store.  I won’t go into verifying to the other data store for this example.

For the purpose of the exercise, create a new C# console project named FileHelpersSample.

Download version 2.0 and reference the FileHelpers library in your project.

Here’s how our CSV looks:

joeuser,joeuser@email.com
awinter,awinter@summer.com
jdoe,jdoe@welcome.org

HINT - If you are following along and creating this project from scratch be sure to create a file named Users.csv with the above entries in it and put it in your debug/release bin folders.

In your project create a new class called User.  This class will be used to map the CSV file.

using System;
using FileHelpers;

namespace FileHelpersSample
{
    [DelimitedRecord(",")]
    public class User
    {
        public string username;
        public string email;
    }
}

Now create a new class and call it Parser.  This class will invoke the FileHelpers engine to parse the file.  It should look like this:

using System;
using System.Collections.Generic;
using FileHelpers;

namespace FileHelpersSample
{
    public class Parser
    {
        private string _filePath;
        public Parser(string filePath)
        {
            _filePath = filePath;
        }

        public User[] Parse()
        {
            FileHelperEngine engine = new FileHelperEngine();
            return engine.ReadFile(_filePath);
        }
    }
}

The FileHelperEngine uses generics to accept the mapping class.  When the ReadFile method is called on the object it uses the mapping to return an array of User objects.

It’s that easy to parse a CSV file.

Now, let’s complete the sample.  Open the Program.cs file and copy the code below:

using System;
using System.Collections.Generic;

namespace FileHelpersSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Parser userParser = new Parser("Users.csv");
            DisplayUsers(userParser.Parse());

            Console.ReadLine(); 
        }

        static void DisplayUsers(User[] list)
        {
            foreach (User u in list)
                Console.WriteLine("{0} {1}", u.username, u.email);   
        }
    }
}

Run the project and you should see:

image 

That was easy! 

The library can do a lot more than this simple example shows.  Be sure to read their quick starts and samples.  The library can ignore the first and last X number of lines as well as empty lines.  It can ignore/process records based on conditions you set along with many other options.  It even has a tool to create your mapping class.

Download this sample in C# or VB.

Happy Coding!

C# | Open Source | Parsing | Programming | VB
Posted 03.20.2010  #    Comments [0]  | 
# Saturday, 14 November 2009

I’m using SyntaxHighlighter and it’s awesome!  It’s completely JavaScript based and can be put into dare I say any blog engine.  If your engine can take JavaScript that is.  I use dasBlog and found two posts on it that guided me nicely.  Give it a shot.  Spruce up your code snippets in your blog.  Everyone will thank you for it.

Scott Hanselman’s blog got me started and Justin Thirkell finished it off.

Anybody writing code on their blog should take a look at SyntaxHighlighter. 

Posted 11.14.2009  #    Comments [0]  |