Encrypting and Decrypting Web.Config file

27. September 2008

There are a few articles pertaining to this subject. I decided to post for archive purposes a class making it easy to do.

/// 
/// Class encrypting and decrypting the web.config
/// 
public class WebProtection
{
    /// 
    /// Protects the section in the web.config.
    /// 
    /// Name of the section to encrypt.
    public static void ProtectSection(string sectionName)
    {
        // Get the configuration
        Configuration config =
            WebConfigurationManager.OpenWebConfiguration("/");

        // Grab the section
        ConfigurationSection section =
            config.GetSection(sectionName);

        // Verify it
        if (section != null && !section.SectionInformation.IsProtected)
        {
            // Encrypt
            section.SectionInformation.ProtectSection(
                "DataProtectionConfigurationProvider");
            // Save
            config.Save();
        }
    }

    /// 
    /// Unprotectss the section in the web.config.
    /// 
    /// Name of the section to decrypt.
    public static void UnProtectSection(string sectionName)
    {
        // Get the configuration
        Configuration config =
            WebConfigurationManager.OpenWebConfiguration("/");

        // Grab the section
        ConfigurationSection section =
            config.GetSection(sectionName);

        // Verify it
        if (section != null && section.SectionInformation.IsProtected)
        {
            // Encrypt
            section.SectionInformation.UnprotectSection();
            // Save
            config.Save();
        }
    }
}

Now all you have to do is call WebProtection.ProtectSection(sectionName) to encrypt a particular part of the web.config file. For connection strings, you would call WebProtection.ProtectSection(“connectionStrings”). I called it from the global.asax.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

, , ,

Developing C# and Outlook 2007

26. September 2008

Overview

This article is going to be a part of a multi-part series on developing with C# and Outlook. The goal of this post is to connect to the Outlook API and create a task with categories. This article assumes you are using Visual Studio 2008 with the .NET Framework version 3.5. These articles are considered proofs-of-concept to aid you in developing better apps than I.

Create a Console Application

Open up visual studio and create a console application. We are creating a console application as apposed to an Outlook plugin to concentrate on the API itself allowing you to develop libraries, services, and plugins for Outlook. So I'm just going to put the basics out there and then explain what everything does.

  • Open up Visual Studio
  • Create a Console Application
  • Right click on the project and select Add Reference
  • Under the .NET tab click Microsoft.Interop.Outlook (the newest version)
  • Click ok

Use this code for the Program.cs file:

using System;
using Microsoft.Office.Interop.Outlook;

/// <summary>
/// This is the class acting as the entry
/// point for the application.
/// </summary>
class Program
{
    /// <summary>
    /// This is the entry point for the application
    /// </summary>
    /// <param name="args">Any arguments
    /// to pass into the application (not used).</param>
    static void Main(string[] args)
    {
        // Create an instance of the outlook application
        Application outlookApp = new ApplicationClass();
        // Get the namespace from the application
        NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");

        // Create a task
        TaskItem task = (TaskItem)outlookApp.CreateItem(OlItemType.olTaskItem);

        // Set up some properties
        task.Subject = "My New Automated Task";
        task.Categories = "@Computer,@Home";

        // Save the task
        task.Save();
    }
}

Explanation

Hopefully the commenting will help the explanation. The first part creates an outlook application and assigns it to the Application interface. This application interface has access to versioning information as well as helper methods. We then get the namespace (folder) in which we are going to add the task to. We are passing in the type (outlook uses MAPI).

The create task is an important one. This line of code can be modified to create mail items, journal items, calendar items, contact items, etc. The outlookApp.CreateItem is a factory method to which we pass in the outlook item type. Finally, we call the save method for the item which places it in to your outlook.

Conclusion

This is a good start to get you on your way. I think this code is simple enough to allow you to fiddle with it and see what you can do. In the near future I will post about manipulating the more complex features of Outlook.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

C#

Making Community Server Private

21. September 2008

There were a couple of people that asked me how to make Community Server exclusive to registered users. My assumption is that there are a lot of articles about this, but I will repost anyways. So here we go.

General Settings

What we are going to do first is set every aspect of community server to be private.

  • Go to the Community Server Control Panel
  • Click on Administration
  • On the left panel, click on Blogs->Global Permissions
  • Find "Everyone" on the grid and uncheck all checkboxes in the row
  • Do the same for Photos, Files, and Forums
  • Click on Membership->New Registration Settings
  • Set account activation to Invite only or Admin approval

Web.Config

To totally disable everyone from even viewing the site unless they are logged in you must open the web.config file in the root directory of your Community Server installation. Add these lines just above the </configuation> at the bottom.

<location path="">
  <system.web>
    <authorization>
      <deny users="?" />
    </authorization>
  </system.web>
</location>

<location path="EmailForgottenPassword.aspx">
  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>

If you are letting people sign up on their own, then add this as well:

<location path="CreateUser.aspx">
  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>

The first location tag disallows everyone, and the second one lets people reset their passwords. I hope this helps.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5