Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, July 18, 2015

RabbitMQ .NET client to connect to WSO2 MB with SSL.

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that provides reliable messaging both secured and unsecured.

This post will explain on how the RabbitMQ .NET client can be used to publish or subscribe to WSO2 Message Broker 3.0.0 securely.

Creating the Certificate

Go to "<MB_HOME>/repository/resources/security" folder and run the following command. This will create a "cert" file which is the certification file for SSL communication. "wso2carbon.jks" is the trust store file. So we are exporting the certificate from the trust store. The "keytool" command comes with the JDK distribution.

keytool -export -keystore wso2carbon.jks -storepass wso2carbon -file carbon.cert -alias localhost -keypass wso2carbon

The Code

You can use the same implementation as mentioned in [1][2]. Only difference is we have to set the following properties to the "ConnectionFactory" object.

........
........
// The connection factory to connect with the broker.
ConnectionFactory factory = new ConnectionFactory();

// AMQP URL to connect to the broker.
IProtocol protocol = Protocols.AMQP_0_9_1;
factory.VirtualHost = "/carbon";
factory.UserName = "admin";
factory.Password = "admin";
factory.HostName = "localhost";
// Port for SSL
factory.Port = 8672;
factory.Protocol = protocol;

// SSL configuration
factory.Ssl.Enabled = true;
factory.Ssl.CertPassphrase = "wso2carbon";
factory.Ssl.CertPath = @"C:\Users\wso2\Documents\wso2mb-3.0.0-ALPHA\repository\resources\security\carbon.cert";
factory.Ssl.ServerName = "localhost";
factory.Ssl.AcceptablePolicyErrors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
factory.Ssl.Version = System.Security.Authentication.SslProtocols.Tls;

using (IConnection conn = factory.CreateConnection())
{
    ............
    ............

Set the correct path for "factory.Ssl.CertPath" to the "cert" file we generated in the earlier step.

Thats all guys!. Click here to download the sample .NET project(VS2010).

References...

[1] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Queue
[2] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Topic
[3] - https://www.rabbitmq.com/ssl.html
[4] - http://pathberiya.blogspot.com/2010_08_01_archive.html

Tuesday, July 9, 2013

Get Start Dates of a Recurring Outlook Appointment

Hi All,

I was able to create a C# stub in order to get the start dates of a recurring appointment in outlook. So far I have not found any errors. Feel free to modify the code if necessary.

private Collection GetOccurrences(DateTime startDate, RecurrencePattern recurrencePattern)
{
    Collection occurrences = new Collection();

    if (recurrencePattern != null && !recurrencePattern.NoEndDate)
    {
        switch (recurrencePattern.RecurrenceType)
        {
            case OlRecurrenceType.olRecursDaily:
                for (int i = 0; i < recurrencePattern.Occurrences; i++)
                {
                    occurrences.Add(startDate);
                    startDate = startDate.AddDays(1 * recurrencePattern.Interval);
                }

                break;
            case OlRecurrenceType.olRecursWeekly:
                if (recurrencePattern.Interval == 0 && (int)recurrencePattern.DayOfWeekMask == 62)
                {
                    int countW = 0;

                    while (countW < recurrencePattern.Occurrences)
                    {
                        if (startDate.DayOfWeek != DayOfWeek.Sunday && startDate.DayOfWeek != DayOfWeek.Saturday)
                        {
                            occurrences.Add(startDate);
                            countW++;
                        }

                        startDate = startDate.AddDays(1);
                    }
                }
                else if (recurrencePattern.Interval > 0)
                {
                    int countW = 0;

                    while (countW < recurrencePattern.Occurrences)
                    {
                        if (recurrencePattern.Interval == 1)
                        {
                            if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, startDate))
                            {
                                occurrences.Add(startDate);
                                countW++;
                            }

                            startDate = startDate.AddDays(1);
                        }
                        else
                        {
                            if (occurrences.Contains(startDate.AddDays(-7)))
                            {
                                startDate = startDate.AddDays(7 * (recurrencePattern.Interval - 1));
                            }

                            if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, startDate))
                            {
                                occurrences.Add(startDate);
                                countW++;
                            }

                            startDate = startDate.AddDays(1);
                        }
                    }
                }

                break;
            case OlRecurrenceType.olRecursMonthly:
                if (recurrencePattern.Instance == 0)
                {
                    for (int i = 0; i < recurrencePattern.Occurrences; i++)
                    {
                        occurrences.Add(startDate);
                        startDate = startDate.AddMonths(recurrencePattern.Interval);
                    }
                }

                break;
            case OlRecurrenceType.olRecursMonthNth:
                occurrences.Add(startDate);
                DateTime dateToAdd = startDate;
                int countMNth = 1;

                while (countMNth < recurrencePattern.Occurrences)
                {
                    dateToAdd = new DateTime(dateToAdd.Year, dateToAdd.AddMonths(recurrencePattern.Interval).Month, 1, dateToAdd.Hour, dateToAdd.Minute, dateToAdd.Second);
                    for (int i = 0; i < 7; i++)
                    {
                        if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, dateToAdd))
                        {
                            int currentMonth = dateToAdd.Month;
                            if (dateToAdd.AddDays(7 * (recurrencePattern.Instance - 1)).Month != currentMonth)
                            {
                                dateToAdd = dateToAdd.AddDays(7 * (recurrencePattern.Instance - 2));
                            }
                            else
                            {
                                dateToAdd = dateToAdd.AddDays(7 * (recurrencePattern.Instance - 1));
                            }

                            occurrences.Add(dateToAdd);
                            countMNth++;
                            break;
                        }
                        else
                        {
                            dateToAdd = dateToAdd.AddDays(1);
                        }
                    }
                }

                break;
            case OlRecurrenceType.olRecursYearly:
                if (recurrencePattern.Instance == 0)
                {
                    int countY = 0;

                    // TODO: Please use a for loop, since the number of iterations is known.
                    while (countY < recurrencePattern.Occurrences)
                    {
                        occurrences.Add(startDate);
                        countY++;
                        //startDate = startDate.AddYears(1);
                        //// Exception popping. please see
                        startDate = startDate.AddYears(recurrencePattern.Interval / 12);
                    }
                }

                break;
            case OlRecurrenceType.olRecursYearNth:
                occurrences.Add(startDate);
                DateTime dateToAddY = startDate;
                int countYNth = 1;

                while (countYNth < recurrencePattern.Occurrences)
                {
                    dateToAddY = new DateTime(dateToAddY.AddYears(recurrencePattern.Interval / 12).Year, dateToAddY.Month, 1, dateToAddY.Hour, dateToAddY.Minute, dateToAddY.Second);
                    for (int i = 0; i < 7; i++)
                    {
                        if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, dateToAddY))
                        {
                            int currentMonth = dateToAddY.Month;
                            if (dateToAddY.AddDays(7 * (recurrencePattern.Instance - 1)).Month != currentMonth)
                            {
                                dateToAddY = dateToAddY.AddDays(7 * (recurrencePattern.Instance - 2));
                            }
                            else
                            {
                                dateToAddY = dateToAddY.AddDays(7 * (recurrencePattern.Instance - 1));
                            }

                            occurrences.Add(dateToAddY);
                            countYNth++;
                            break;
                        }
                        else
                        {
                            dateToAddY = dateToAddY.AddDays(1);
                        }
                    }
                }

                break;
            default:
                break;
        }
    }

    return occurrences;
}

private bool IsMaskedDay(int maskedInt, DateTime date)
{
    string binaryString = Convert.ToString(maskedInt, 2);
    binaryString = new string('0', 7 - binaryString.Length) + binaryString;

    char[] binaryArray = binaryString.ToCharArray();
    Array.Reverse(binaryArray);

    return binaryArray[(int)date.DayOfWeek] == '1';
}

Delete Social Tags in SharePoint 2010 using C#

Hi all,
I've recently wanted to delete social tags when I am deploying my wsp using powershell. I started writing the code and was able to retrieve the social tags but was not able to delete them. When I attempted to delete them an exception was thrown. More like an Unauthorized exception. Read this for more information on it.

But I was able to find this blog post which helped me out in solving the issue.  I was able to create a console program using C# to delete the social tags of SharePoint 2010. I havent tested it much. Feel free to modify the code.
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Web;
using Microsoft.Office.Server.SocialData;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace RemoveTags
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite(args[0]))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Deleting all tags");
                    foreach (SPUser user in site.OpenWeb().AllUsers)
                    {
                        if (!user.IsDomainGroup && user.LoginName != "NT AUTHORITY\\LOCAL SERVICE" && user.LoginName != "SHAREPOINT\\system")
                        {
                            web.AllowUnsafeUpdates = true;
                            Console.WriteLine("Deleting tags for : " + user.Name);
                            HttpRequest request = new HttpRequest("", args[0], "");
                            HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter(CultureInfo.CurrentCulture)));
                            HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
                            WindowsIdentity wi = WindowsIdentity.GetCurrent();
                            typeof(WindowsIdentity).GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(wi, user.LoginName);
                            HttpContext.Current.User = new GenericPrincipal(wi, new string[0]);
                            WindowsIdentity wi2 = WindowsIdentity.GetCurrent();
                            SPServiceContext serviceContext = SPServiceContext.GetContext(HttpContext.Current);
                            SocialTagManager socialTagManager = new SocialTagManager(serviceContext);
                            SPServiceContext context = SPServiceContext.GetContext(site);
                            UserProfileManager usrPrfMngr = new UserProfileManager(context);
                            UserProfile usrPrf = usrPrfMngr.GetUserProfile(user.LoginName);
                            SocialTag[] tags = socialTagManager.GetTags(usrPrf);

                            foreach (SocialTag tag in tags)
                            {
                                Term term = tag.Term;
                                socialTagManager.DeleteTag(tag.Url, term);

                            }
                        }
                    }

                    web.AllowUnsafeUpdates = false;

                    Console.WriteLine("All tags are deleted");
                    Console.ResetColor();
                    Console.ReadLine();
                }
            }
        }
    }
}

Sunday, March 17, 2013

Force Printer Cleaner

Hello People,

I believe all of you have been stuck on printers where you send files to the printer, But they do not actually print. And later pm you try to delete the file to be printed in the printer tray and they just keep on saying deleting.

Well here is an executable file that will remove the files in the printer tray. No more printer troubles. :D

Force Printer Cleaner

Thanks,
Hemika Kodikara

Tuesday, March 12, 2013

Dummy File Creator

Hello guys,

Herewith I have created a windows application to create empty files with a specific file size. This tool can be used for testing purposes of file upload control of web applications and etc.

Dummy File Creator

Thanks :)

Monday, January 28, 2013

Windows form application to run a PowerShell script file.

Hello Guys,
I have developed a small windows form application to run a PowerShell script file. I havent done many of windows form applications.

The application will only run a script. It will not call any function on the script unless the script it self is calling the functions.
The command "write-host" will give an error if it mentioned on the script. Therefore change the "write-host" commands to "write-output" in your script. "write-output" will actually send an output to the text window.

Feel free to edit the software.


A sample ps1 file is also included.

Download File