dotnet thoughts 

a dotnet developer's technical blog

Implementing ASP.Net Forms Authentication with Active Directory Membership Provider

ActiveDirectoryMembership provider is used manage users against Active Directory, which helps to create Single Sign On for intranet application. Here is a basic implementation, which used to Authenticate users against Active Directory, using Login Control.

We need to modify the web.config, like the implementation of Forms Authentication in ASP.Net.

Create / Add a connection string to active directory database.

<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://DOMAIN.SUBDOMAIN/DC=DOMAIN,DC= SUBDOMAIN "/>
</connectionStrings>

Configure Membership node in the web.config with ActiveDirectoryMembershipProvider.

<membership defaultProvider="MembershipADProvider">
  <providers>
    <add name="MembershipADProvider"
         type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
         applicationName="dotnetthoughts"
         connectionStringName=" ADConnectionString "
         attributeMapUsername="sAMAccountName"/>
  </providers>
</membership>

You can also provide the Username / Password in this using connectionUsername , connectionPassword attributes.

Modify the Authentication mode and Authorization nodes for controlling the access permissions. Currently I am using Forms Authentication defaults. (default.aspx – Home Page, and Login.aspx – Login Page)

<authentication mode="Forms" />
<authorization>
    <deny users="?" />
    <allow users="*" />
</authorization>

Almost done. Now drag and drop Login control from Toolbox > Login tab to Login.aspx. Run the application, say OK to the Debug mode confirmation from Visual Studio. As we are configured the Authentication provider we don’t need to write any Code to Authentication.

If you don’t want to use login control, you can do something like this in the code behind for the authentication.

if (Membership.ValidateUser(this.txtUsername.Text, this.txtPassword.Text))
{
    FormsAuthentication.RedirectFromLoginPage(this.txtUsername.Text, false);
}
else
{
    Response.Write("Authentication failed.\nUsername / Password Invalid");
}

Thanks to Sreenaja for the initial implementation. Happy Coding :)

DevCon 2010 – 3rd and 4th July 2010 – Trivandrum

DevCon 2010 Logo

DevCon 2010

The Kerala Microsoft Users Group (K-MUG) will be organising a Developers Conference, DevCon 2010 on July 03 & 04, 2010 at ParkCenter,Technopark in Trivandrum. K-MUG is one of the best developer community in India, which got Best User Group award in India as part of Microsoft Community Impact award 2010. DevCon 2010 will help you to engage and collaborate with Microsoft MVPs and thousands of your IT peers, building connections that will last beyond your two days at DevCon.

Sessions

Day 1 (3rd July – Saturday)

08:30AM – 09:15AM – Registration Confirmation
09:15AM – 09:30AM – Welcome Speech
09:30AM – 10:15AM – Key Note Session – “Cloud – The Meta Platform”
10:15AM – 11:00AM – New features in .NET 4.0 & Visual Studio 2010

11:00AM – 11:15AM – Tea break

11:15AM – 12:15PM – Robotics Programming
12:15PM – 01:15PM – Web Security and Security Auditing

01:15PM – 02:15PM – Lunch

02:15PM – 03:15PM – Windows Azure
03:15PM – 04:00PM – Great Developer Contest – Final
04:00PM – 05:00PM – Managing Application Compatibility in Windows 7

Day 2 (4th July – Sunday)

08:30AM – 09:30AM – Registration Confirmation
09:30AM – 10:30AM – Data on the Cloud
10:30AM – 11:30AM – Mixed Mode Windows development using C# and C++

11:30AM – 11:45AM – Tea Break

11:45AM – 12:00PM – Visual Studio 2010 tips
12:30PM – 01:30PM – Tuning Tools in SQL Server 2008

01:30PM – 02:30PM – Lunch

02:30PM – 03:15PM – ASP.NET MVC2
03:15PM – 04:00PM – Windows 7 Phone
04:00PM – 05:00PM – Closing Ceremony

Speakers

Janakiram MSV works with Microsoft Corporation as the Technical Architect.

Ramaprasanna Chellamuthu works with Microsoft as a Developer Evangelist.

Manu Zacharia is an Information Security evangelist with more than 16 years of professional experience and MVP in Enterprise Security.

Praseed Pai is a well known software architect from Kochi,Kerala.

Anoop Madhusoodhanan is a Solution Architect at UST Global and a Microsoft MVP in Client App Development.

Sreejumon KP is the founder and Community Council member of Kerala Microsoft User group (www.k-mug.org).

Vijay Raj works at Texas Instruments Bangalore focusing on Application Setup and Deployment.

Jeen Shene Stanislaus is a Technical Evangelist with Education & Research, Infosys, Trivandrum.

Shoban Kumar is currently working with an MNC in Trivandrum as a Senior Software Engineer.

Shiju Varghese is a Technical Architect on the Microsoft .Net technology stack with a focus on web application development and Domain-Driven Design.

Contests

Participate in the great Developer contest and win cool prizes. Do you have a killer solution idea related to Twitter? Do you think you can put better use of Microsoft Tools and Technologies? Then “The Great Developer Challenge” is the perfect game for you. Showcase your idea related to Twitter and win Cool prizes.

Venue

ParkCenter, Technopark, Trivandrum, Kerala, India

Find more details here : http://k-mug.org/events/devcon2010/

Customize SpellCheck in WPF textbox

While working on a personal project (fleetIt). I worked on Textbox spell check option. I enabled it, it was working fine, until I added a context menu to to the textbox, for custom commands of my application. It was displaying the spelling errors but the suggestions and correction options was not available. Then I tried to customize the context menu such a way that it can display both my custom options as well as the system default spell check options. Here is a simple implementation, which helps to customize the context menu and display the spell check suggestions options.

<Window x:Class="dotnetthoughts.net.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=System"
Title="WPF SpellCheck Demo" Height="350" Width="525" Loaded="Window_Loaded">
<DockPanel>
<TextBox Name="txtEdit" SpellCheck.IsEnabled="True"
                AcceptsReturn="True"
ContextMenuOpening="txtEdit_ContextMenuOpening"
                VerticalScrollBarVisibility="Visible">
<TextBox.ContextMenu>
<ContextMenu Name="ctxMenu" />
</TextBox.ContextMenu>
</TextBox>
</DockPanel>
</Window>

And here is the code behind

namespace dotnetthoughts.net
{
    using System;
    using System.IO;
using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;

public partial class MainWindow : Window
    {
public MainWindow()
        {
InitializeComponent();
        }

private void txtEdit_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            int index = 0;
this.txtEdit.ContextMenu.Items.Clear(); //Clearing the existing items
            //Getting the spellcheck suggestions.
SpellingError spellingError = this.txtEdit.GetSpellingError(this.txtEdit.CaretIndex);
if (spellingError != null && spellingError.Suggestions.Count() >= 1)
            {
                //Creating the suggestions menu items.
foreach (string suggestion in spellingError.Suggestions)
                {
MenuItem menuItem = new MenuItem();
                    menuItem.Header = suggestion;
menuItem.FontWeight = FontWeights.Bold;
menuItem.Command = EditingCommands.CorrectSpellingError;
                    menuItem.CommandParameter = suggestion;
menuItem.CommandTarget = this.txtEdit;
this.txtEdit.ContextMenu.Items.Insert(index, menuItem);
                    index++;
                }
Separator seperator = new Separator();
this.txtEdit.ContextMenu.Items.Insert(index, seperator);
                index++;
//Adding the IgnoreAll menu item
MenuItem IgnoreAllMenuItem = new MenuItem();
                IgnoreAllMenuItem.Header = "Ignore All";
IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError;
IgnoreAllMenuItem.CommandTarget = this.txtEdit;
                this.txtEdit.ContextMenu.Items.Insert(index, IgnoreAllMenuItem);
                index++;
            }
            else
            {
//No Suggestions found, add a disabled NoSuggestions menuitem.
                MenuItem menuItem = new MenuItem();
menuItem.Header = "No Suggestions";
                menuItem.IsEnabled = false;
this.txtEdit.ContextMenu.Items.Insert(index, menuItem);
                index++;
            }
//.Net 4.0 Supports CustomDictionaries, Option for Adding to dictionary.
int selectionStart = this.txtEdit.GetSpellingErrorStart(this.txtEdit.CaretIndex);
            if (selectionStart >= 0)
            {
                Separator seperator1 = new Separator();
this.txtEdit.ContextMenu.Items.Insert(index, seperator1);
                index++;
MenuItem AddToDictionary = new MenuItem();
                AddToDictionary.Header = "Add to Dictionary";
                //Getting the word to add
this.txtEdit.SelectionStart = selectionStart;
this.txtEdit.SelectionLength = this.txtEdit.GetSpellingErrorLength(this.txtEdit.CaretIndex);
                //Ignoring the added word.
AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
AddToDictionary.CommandTarget = this.txtEdit;
AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
                {
this.AddToDictionary(this.txtEdit.SelectedText);
                };
this.txtEdit.ContextMenu.Items.Insert(index, AddToDictionary);
                index++;
            }

//Common Edit MenuItems.
            Separator seperator2 = new Separator();
this.txtEdit.ContextMenu.Items.Insert(index, seperator2);
            index++;
            //Cut
MenuItem cutMenuItem = new MenuItem();
cutMenuItem.Command = ApplicationCommands.Cut;
this.txtEdit.ContextMenu.Items.Insert(index, cutMenuItem);
            index++;
            //Copy
MenuItem copyMenuItem = new MenuItem();
copyMenuItem.Command = ApplicationCommands.Copy;
this.txtEdit.ContextMenu.Items.Insert(index, copyMenuItem);
            index++;
            //Paste
MenuItem pasteMenuItem = new MenuItem();
pasteMenuItem.Command = ApplicationCommands.Paste;
this.txtEdit.ContextMenu.Items.Insert(index, pasteMenuItem);
            index++;
            Separator seperator3 = new Separator();
this.txtEdit.ContextMenu.Items.Insert(index, seperator3);
            index++;
            //Delete
MenuItem deleteMenuItem = new MenuItem();
deleteMenuItem.Command = ApplicationCommands.Delete;
            this.txtEdit.ContextMenu.Items.Insert(index, deleteMenuItem);
            index++;
            Separator seperator4 = new Separator();
this.txtEdit.ContextMenu.Items.Insert(index, seperator4);
            index++;
            //Select All
MenuItem selectAllMenuItem = new MenuItem();
selectAllMenuItem.Command = ApplicationCommands.SelectAll;
            this.txtEdit.ContextMenu.Items.Insert(index, selectAllMenuItem);
            index++;
        }
        //Method to Add text to Dictionary
private void AddToDictionary(string entry)
        {
using (StreamWriter streamWriter = new StreamWriter(@"D:\WPF\MyCustomDictionary.lex", true))
            {
streamWriter.WriteLine(entry);
            }
        }

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
//Assigning custom Dictionary to TextBox
this.txtEdit.SpellCheck.CustomDictionaries.Add(new Uri(@"D:\WPF\MyCustomDictionary.lex"));
        }
    }
}

The .Net Framework 4.0 supports CustomDictionaries, which helps to create your own Dictionary.

//.Net 4.0 Supports CustomDictionaries, Option for Adding to dictionary.
int selectionStart = this.txtEdit.GetSpellingErrorStart(this.txtEdit.CaretIndex);
if (selectionStart >= 0)
{
    Separator seperator1 = new Separator();
this.txtEdit.ContextMenu.Items.Insert(index, seperator1);
    index++;
MenuItem AddToDictionary = new MenuItem();
    AddToDictionary.Header = "Add to Dictionary";
    //Getting the word to add
this.txtEdit.SelectionStart = selectionStart;
this.txtEdit.SelectionLength = this.txtEdit.GetSpellingErrorLength(this.txtEdit.CaretIndex);
    //Ignoring the added word.
AddToDictionary.Command = EditingCommands.IgnoreSpellingError;
AddToDictionary.CommandTarget = this.txtEdit;
AddToDictionary.Click += (object o, RoutedEventArgs rea) =>
    {
this.AddToDictionary(this.txtEdit.SelectedText);
    };
this.txtEdit.ContextMenu.Items.Insert(index, AddToDictionary);
    index++;
}

Here is the screenshot of Demo Application.

WPF Spell Check Demo - Screenshot

WPF Spell Check Demo - Screenshot

Custom Places in FileDialog box

If you ever tried to Open a File from Visual Studio, you may notice something like Projects Folder in the Open File Dialog.

Open File Dialog in Visual Studio

Open File Dialog in Visual Studio

We can also implement the same functionality in our applications by using CustomPlaces collection property of FileDialog class. The OpenFileDialog and SaveFileDialog classes allow you to add folders to the CustomPlaces collection.

openFileDialog.CustomPlaces.Add(@"C:\Users");

You can also specify GUID of Windows Vista known folder. Known Folder GUIDs are not case sensitive and are defined in the KnownFolders.h file in the Windows SDK. If the specified Known Folder is not present on the computer that is running the application, the Known Folder is not shown.

openFileDialog.CustomPlaces.Add(@"C:\Users");
//Desktop Folder
openFileDialog.CustomPlaces.Add(new Guid("B4BFCC3A-DB2C-424C-B029-7FE99A87C641"));
//Downloads Folder
openFileDialog.CustomPlaces.Add(new Guid("374DE290-123F-4565-9164-39C4925E467B"));
Open File Dialog with Custom Places

Open File Dialog with Custom Places

Note: This feature will not have any effect in Windows XP. Also you must set the AutoUpgradeEnabled to True(default) to enable this feature in Vista or Windows 7.

The following table lists few Windows Vista Known Folders and their associated Guid.

  1. Contacts : 56784854-C6CB-462B-8169-88E350ACB882
  2. ControlPanel : 82A74AEB-AEB4-465C-A014-D097EE346D63
  3. Desktop : B4BFCC3A-DB2C-424C-B029-7FE99A87C641
  4. Documents : FDD39AD0-238F-46AF-ADB4-6C85480369C7
  5. Downloads : 374DE290-123F-4565-9164-39C4925E467B
  6. Favorites : 1777F761-68AD-4D8A-87BD-30B759FA33DD
  7. Fonts : FD228CB7-AE11-4AE3-864C-16F3910AB8FE
  8. Music : 4BD8D571-6D19-48D3-BE97-422220080E43

DevCon 2010 by Kerala Microsoft User Group at Technopark Trivandrum

DevCon 2010 Logo

DevCon 2010

Kerala Microsoft User Group – K-MUG – coming with another mega event named DevCon 2010. Still we are finalizing the speakers and topics. But you will get some idea about the topics and sessions now. We have some contests as well for the developers, and try to get free Nokia touch phone if you submit the best entry.

For more details visit

http://k-mug.org/events/DevCon2010/

Why attend DevCon 2010?

Because you’ll return to the office with cutting-edge insights and expertise that will make life easier for you (and everyone else) at work. Immerse yourself in IT learning opportunities and get your questions answered by renowned technology experts. Even more importantly, engage and collaborate with Microsoft MVPs and thousands of your IT peers, building connections that will last beyond your two days at DevCon.

When

03 (Saturday) and 04 (Sunday) – July – 2010

Where

ParkCenter, Technopark, Trivandrum, Kerala, India

How to register

Watch this thread for more information : http://k-mug.org/forums/p/1499/4491.aspx. And you can register using this link :Register

Older Posts »