Showing all posts tagged: 'c#'

A 23-post collection

A simple .NET password hashing implementation using BCrypt

By now, you've heard many many stories about compromised sites and how millions of emails and clear-text passwords have made it to the hands of "not so good" people. If you are a developer and you need to create some kind of authentication for your clients/software/site/pet-project, please make sure you approach this with the gravity that it demands. Troy Hunt, a security expert has written about the subject multiple times and I would urge you seriously to have a look at his blog or his pluralsight courses. Troy is one of the many security experts …[read more]


WebApi controller action with comma-separated parameter values

Today I've run across an issue where I had to create a WebAPI GET action that could accept, among other things, a parameter with comma-separated values. If you ever need to implement this, then that's one way to do it: Create an action filter### The action filter will be used to intercept the action call and process the comma-separated parameter. Then, you simply assing the action filter to the controller action and you can carry on with the next task at hand. A practical example### First, let's create the action filter. Create a new class and name it something like …[read more]


Getting Json data using jQuery and .ASMX web services.

If you still have the misfortune to work with asmx services on your project, you may want to know how to easily retrieve json data from an asmx method. The differences between asmx and WCF have been aalyze[d b]efore here and here. Return a simple string as json response:### Your asmx page code#### [WebService(Namespace = "http://contoso.com/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]System.Web.Services.WebService [ScriptService] public class services :WebService { [WebMethod(CacheDuration = 60)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetGreeting() { return "hello world" } } Your javascript code#### …. code omitted … var getGreeting = function() { var …[read more]


Ordering Collections with enum properties using LINQ

Posted in Linq, c#

Interesting find today. To order a collection based on an enum alphabetically, you can use LINQ as per the example below: var collection = new List<myCustomObjectWithEnumProperty>(); var sortedCollection = collection.OrderBy(c=>c.EnumProperty.ToString()); To order a collection based on an enum by value, you can do the following: var collection = new List<myCustomObjectWithEnumProperty>(); var sortedCollection = collection.OrderBy(c=>(int)c.EnumProperty); Simple as that! Happy coding... …[read more]


Processing payments on your site using Stripe and C#

Update: The current example of processing payments is NOT PCI-compliant as the card details are posted to the server. A better approach is to use the Stripe.js to generate the token and then process the payment - thanks to Christian Bryant for raising it in the comments. Take me to the PCI-Compliant option now As a web developer you are bound to have come across a requirement to implement some form of online payment processing for a site. This can be achieved using any number of third party providers such as Sage or PayPal etc. Although most of these …[read more]


Fixing TextChanged event firing twice in Windows Phone 7 (WP7)

I’m currently playing around with WP7 and WP8 development trying to get to grips with the framework. I don’t have a specific application in mind but I’m trying to create a basic application that allows me take notes on the fly. Nothing fancy there. One of the “requirements” is to be able to search across all my notes for a specific word and get all the results back in one continuous list that will contain the name/title of the note that matches the search criteria. In my effort to be fancy, I decided against using a …[read more]


An Object with the same key already exists in the ObjectStateManager, Entity Frawework

I have a generic repository which I use in my application to manage a large number of objects. I wrote the necessary unit tests to ensure that all methods work as expected and in isolation all was good. However, as soon as the application started implementing more complex operations, I came across the following error during updates: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.” In my controller I do something along these lines: Nothing unusual here. Move on, nothing to see. I fetch the existing user …[read more]


XML Validation Failure Due To Invincible White Space

After spending 3 hours of my life trying to resolve the following error message: The element cannot contain white space. Content model is empty I decided to share my solution in hope that this will help somebody else. The xml file that was failing validation is: <?xml version="1.0" encoding="utf-8"?> <ComponentSet Default="Create Data Set"> <Procedures> <Procedure Name="Create Data Set"> <MainFlow> <CreateDataSetVersion Name="run create data set version task" Description="this the description" CollectionId= …[read more]