Skip to content

Instantly share code, notes, and snippets.

@JoanComasFdz
JoanComasFdz / MyStringEnum.cs
Created December 28, 2011 09:14
An enumerator example type with string values.
/// <summary>
/// This is an example of how to implement
/// an string enum.
/// </summary>
public enum MyStringEnum
{
[StringValue("This is the string value 1.")]
Member1,
[StringValue("This is the string value 1.")]
Member2,
@JoanComasFdz
JoanComasFdz / StringEnum.cs
Created December 28, 2011 09:14
A class that uses generics to parse and get a value from an enumerator member with a StringValueAttribute.
/// <summary>
/// Provides the tools needed to get in a easy, fast way
/// the string value of an enumerator member assigned
/// with <see cref="StringValueAttribute"/>.
/// </summary>
/// <typeparam name="E">The type of the enumerator.</typeparam>
public static class StringEnum<E>
{
/// <summary>
/// Gets the enumerator member's string value.
@JoanComasFdz
JoanComasFdz / StringValueAttribute.cs
Created December 28, 2011 09:13
Attribute to contain a string value
/// <summary>
/// This attribute allows to attach to any
/// class, method, property or enumerator member
/// a string value.
/// </summary>
public class StringValueAttribute : Attribute
{
/// <summary>
/// Returns the attached string value.
/// </summary>
@JoanComasFdz
JoanComasFdz / MyNotImplementableStringEnum.cs
Created December 28, 2011 09:13
Not-implementable string enum
public enum MyStringEnum
{
Text1 = "My text 1",
}
@JoanComasFdz
JoanComasFdz / gist:1527200
Created December 28, 2011 09:05
Basic console program to acces a WCF service via using username and password.
static void Main(string[] args)
{
Service1Client client = null;
try
{
// Create the client
// It will use the settings in the app.config file
client = new Service1Client();
// Set credentials
client.ClientCredentials.UserName.UserName = "USER_PASSWORD";
@JoanComasFdz
JoanComasFdz / gist:1527198
Created December 28, 2011 09:05
Basic binding for a WCF service client using username and password.
<client>
<endpoint address="YOUR_SERVICE_URL"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_YOUR_REFERENCED_SERVICE_INTERFACE"
contract="YOUR_REFERENCED_SERVICE.YOUR_REFERENCED_SERVICE_INTERFACE"
name="NetTcpBinding_YOUR_REFERENCED_SERVICE_INTERFACE">
<identity>
<dns value="dev_cert_2"/>
<certificate encodedValue="..." />
</identity>
@JoanComasFdz
JoanComasFdz / gist:1527196
Created December 28, 2011 09:04
Basic service behaviour with username and password authentication
<behaviors>
<serviceBehaviors>
<behavior name ="beh_auth">
<serviceMetadata/>
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="YOUR_HOST_NAMESPACE.YOUR_CUSTOM_VALIDATOR, YOUR_ASSEMBLY_NAME"/>
<serviceCertificate
findValue="YOUR_CERT_NAME"
@JoanComasFdz
JoanComasFdz / gist:1527190
Created December 28, 2011 09:02
Basic netTcpBinding with message-based security using username and password
<bindings>
<netTcpBinding>
<binding name="tcp_auth"
portSharingEnabled="false">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</netTcpBinding>
</bindings>
@JoanComasFdz
JoanComasFdz / gist:1527187
Created December 28, 2011 09:01
Basic custom UserNamePasswordValidator implementation
public class CustomValidator : UserNamePasswordValidator
{
public override void Validate(string user_name, string password)
{
if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(password))
throw new SecurityTokenException("Username and password required");
if (!(user_name == "test" && password == "test"))
throw new SecurityTokenException("Wrong username or password.");
// Not throwing exceptions indicates
@JoanComasFdz
JoanComasFdz / Program.cs
Created December 28, 2011 08:50
Client for wcf service in console app basic code (using dual tcp to allow callbacks)
using System;
using System.ServiceModel;
using Client_Service_with_callbacks_via_tcp.<YOUR_SERVICE_NAME>;
namespace Client_Service_with_callbacks_via_tcp
{
class Program
{
/// <summary>
/// Handles the callbacks from the server following