Posts Tagged .net

Learn how to develop an Angry Birds look-alike for windows phone 7

Recently a colleague of mine (Jo De Greef) has written some pretty extensive articles on how you can create your own Angry Birds look-alike application for the Windows Phone 7 platform. In a series of posts he will explain the techniques you need to master to create your own version of the popular game. Of course you can use these techniques for entire different purposes.

The work on the series is still in progress at te moment. For now you can already checkout 2 awesome articles:

If you like what Jo is doing let him know on Twitter: http://twitter.com/jodegreef or regularly visit his blog at http://jodegreef.wordpress.com

, , , ,

No Comments

How to get the week number for a date in C#

Today I needed to know which week of the year a certain day was in. My first instinct was DateTime.Now.Week but it seems there is no Week property on a DateTime instance. So I started looking for a way to get the week number. As it turns out the CultureInfo class will get you where you want to go using the Calendar property.

I updated my extensions library (How to get the mime type of a file using the name of a file in C# and PART 2: How to get the mime type of a file using the name of a file in C# ) to add an extension method called WeekNumber for a DateTime instance here is the code:


    public static class DateTimeExtensions
    {
        public static int WeekNumber(this System.DateTime value)
        {
            return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(value, CalendarWeekRule.FirstFourDayWeek,
                                                                     DayOfWeek.Monday);

        }
    }

You can download my updated extension library here : Suddenelfilio.ExtensionMethods.zip
Downloaded 204 times

, , , ,

No Comments

How to calculate password hash in C# for Jira users?

While I’m working on migrating our current issue tracking system to the new Jira instance I needed to manually create new users in Jira’s database. The users are stored in the userbase table, but when you take a look you’ll see that the password is not stored instead there is a column called “PASSWORD_HASH” which contains the password in a hashed form. Since Jira uses the OSUser package from OpenSymphony you can go and look in the code.

The code that Jira uses to create the password hash is the following in Java:


private String createHash(String original) {
   byte[] digested = PasswordDigester.digest(original.getBytes());
   byte[] encoded = Base64.encode(digested);

   return new String(encoded);
}

Now here you a digest being generated from the original value. Well this is a SHA-1/512 hash which is then converted to a Base64 representation and that’s all!

Okay here is how you can do this in C#:


using System;
using System.Text;
using System.Security.Cryptography;

public sealed class JiraPasswordHasher
{

    public static string createHash(string original)
    {
        SHA512 shaM = new SHA512Managed();
        var result = shaM.ComputeHash(System.Text.Encoding.ASCII.GetBytes(original));

        return Convert.ToBase64String(result);

    }
}

Have fun!

, , , , , , , , , ,

1 Comment

LinkedIn OAuth using Hammock in C# & ASP.NET

UPDATE: Hammock moved to github https://github.com/danielcrenna/hammock

 

Codeplex.com description of Hammock:
Hammock is a REST library for .NET that greatly simplifies consuming and wrapping RESTful services.

I’m currently working on a little web project that needed integration with LinkedIn.com. The LinkedIn API allows for OAuth authorization and authentication. They describe the process of getting a request token, authorizing it by the user and then getting an access token. Standard OAuth you might say. So I needed a way to do this in C# and found the Hammock library. Although Hammock does not only do OAuth I used it for that purpose only at the moment.

Below is the code I wrote to get a request token and send the user to the authorization page at Linkedin:


public void RequestAndAuthorize()
        {
            var credentials = new OAuthCredentials
            {
                CallbackUrl = "http://127.0.0.1/oauth/callback/",
                ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
                Verifier = "123456",
                Type = OAuthType.RequestToken
            };
            var client = new RestClient { Authority = "https://api.linkedin.com/uas/oauth", Credentials = credentials };
            var request = new RestRequest { Path = "requestToken" };
            RestResponse response = client.Request(request);

            string token = response.Content.Split('&').Where(s => s.StartsWith("oauth_token=")).Single().Split('=')[1];
            string token_secret = response.Content.Split('&').Where(s => s.StartsWith("oauth_token_secret=")).Single().Split('=')[1];

            Response.Redirect("https://api.linkedin.com/uas/oauth?oauth_token=" + token);
        }

Once the user has authorized your request token the LinkedIn server will redirect the user back to the callback url in this case this is http://127.0.0.1/oauth/callback. Using the returned oauth_token and oauth_token_secret (you got this one while requesting a request token) you can now get an access token so you can start making authenticated API calls from you application on behalf of the user.

This is the code that is used when the callback url is called:


	public void Callback()
        {
            var credentials = new OAuthCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
                Token = token,
                TokenSecret = token_secret,
                Verifier = verifier,
                Type = OAuthType.AccessToken,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                Version = "1.0"
            };
            var client = new RestClient { Authority = "https://api.linkedin.com/uas/oauth", Credentials = credentials, Method = WebMethod.Post };
            var request = new RestRequest { Path = "accessToken" };
            RestResponse response = client.Request(request);
            string content = response.Content;
        }

As you can see Hammock is really nice and allows for easy OAuth authentication/authorization to be used.

, , , , , , , ,

23 Comments

UPDATE: MS Tag REST service issues

During the last 24 hours the service has been experiencing some issues when called. This has currently been resolved. If you’ve received an http 500 error code when calling the service please try again. The issue was introduced when I extended the service to record certain metrics. These metrics or stats can be seen on http://tag.ws.suddenelfilio.net/ at the end of the page.

If you experience any other problems please contact me at: mstaglib@suddenelfilio.net

, , , ,

No Comments

MS Tag REST Certificate Issues

This week I received an email telling me that they got an error while using the MS Tag REST  service:

System.ServiceModel.Security.SecurityNegotiationException: Could not establish trust relationship for the SSL/TLS secure channel with authority ‘ws.tag.microsoft.com‘.

As you can see the authority is complaining about the Microsoft domain. The issue was the certificate used by Microsoft’s Tag API was outdated and thus the error was thrown when the  REST service communicated with the Microsoft service.

Since the certificate has been renewed and is now valid till somewhere early 2011 this problem should be resolved.

, , ,

No Comments

Update: MS Tag REST relocation

Today the service has been transferd to the new server. I’ve update the dns settings for the domain tag.ws.suddenelfilio.net so this should be a rather painless transfer if you are using the service. Please feel free to contact me if there are any problems.

http://tag.ws.suddenelfilio.net for more information.

, ,

1 Comment

MS Tag REST relocation

UPDATE: The service has been relocated.

I will soon be moving the MS Tag REST API to my new server. Currently this is hosted at Windows Azure, but I don’t want to risk an unwanted and unexpected bill being served because I overused the azure service. Since I currently don’t track the usage I decided to move it.

I will announce the move on codeplex, getsatisfaction.com and the tag site at ning.com During the migration I will try to keep this painless as possible and with the minimum amount of download. It will be a matter of DNS propagation.

So if you are using the MS Tag REST API please leave a comment and I will inform you personally when the transfer will happen.

, , ,

2 Comments

Outlook Social Connector for Twitter

I’ve been testing the new version of Office 2010 (or Office 14) for some time now. One of the new features in the Outlook program is the possibility to integrate with social networks through the means of social providers. Currently out-of-the-box there is a provider for Microsoft Sharepoint and a strategic partnership with LinkedIn to provide a provider for their network.

However you are free to develop your own osc (outlook social connector) providers. Currently I’m trying to write one for integrating Twitter. It’s going nicely, but it’s not finished yet. I’ve hit some bumps along the road and with some feedback from the friendly devs over at Microsoft I’m now finishing the first version of an ‘Outlook Social Connector for Twitter’. When it is finished I promise to release the entire code on Codeplex as a real world reference sample for other developers to create osc providers.

, , , , ,

7 Comments