Monday, July 16, 2012

Serialization using C#

Serialization using C#


(Source: http://msdn.microsoft.com/en-us/library/ms233843.aspx)

Serialization is the process of converting an object into a stream of bytes in order to persist it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
process is called deserialization.
This illustration shows the overall process of serialization.
Serialization Graphic The object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.

Uses for Serialization

Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.

Making an Object Serializable

To serialize an object, you need the object to be serialized, a stream to contain the serialized object, and a Formatter. System.Runtime.Serialization contains the classes necessary for serializing and deserializing objects.
Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. A SerializationException exception is thrown if you attempt to serialize but the type does not have the SerializableAttribute attribute.
If you do not want a field within your class to be serializable, apply the NonSerializedAttribute attribute. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and the field cannot be meaningfully reconstituted in a different environment, then you may want to make it nonserializable.
If a serialized class contains references to objects of other classes that are marked SerializableAttribute, those objects will also be serialized.
Either binary or XML serialization can be used. In binary serialization, all members, even those that are read-only, are serialized, and performance is enhanced. XML serialization provides more readable code, as well as greater flexibility of object sharing and usage for interoperability purposes.

Binary Serialization

Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams.

XML Serialization

XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to XML. System.Xml.Serialization contains the classes necessary for serializing and deserializing XML.
You can apply attributes to classes and class members in order to control the way the XmlSerializer serializes or deserializes an instance of the class.

SOAP Serialization

XML serialization can also be used to serialize objects into XML streams that conform to the SOAP specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML. As with regular XML serialization, attributes can be used to control the literal-style SOAP messages generated by an XML Web service.
Serialization can be performed in two ways, basic and custom. Basic serialization uses the .NET Framework to automatically serialize the object.

Basic Serialization

The only requirement in basic serialization is that the object has the SerializableAttribute attribute applied. The NonSerializedAttribute can be used to keep specific fields from being serialized.
When you use basic serialization, the versioning of objects may create problems, in which case custom serialization may be preferable. Basic serialization is the easiest way to perform serialization, but it does not provide much control over the process.

Custom Serialization

In custom serialization, you can specify exactly which objects will be serialized and how it will be done. The class must be marked SerializableAttribute and implement the ISerializable interface.
If you want your object to be deserialized in a custom manner as well, you must use a custom constructor.
Designer serialization is a special form of serialization that involves the kind of object persistence usually associated with development tools. Designer serialization is the process of converting an object graph into a source file that can later be used to recover the object graph. A source file can contain code, markup, or even SQL table information. For more information, see Designer Serialization Overview.

Tuesday, July 10, 2012

Silverlight Programming

For demo site of how silverlight works, please visit http://www.silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html
They have a very good demo there

To Begin, the following are the software requirements
  • Visual Studio 2010 or later (.net framework 4.0)
  • Silverlight Developer (should install silverlight 5 or higher)
  • Silverlight toolkit for those who are not ready to code through xaml
  • Basic knowledge of xaml and html
You are Ready to begin. We'll start with a simple animated hello world application
Watch this space for posts and comments

Wednesday, June 13, 2012

Open ID Authentication - C# ASP.NET: Getting Started

Open ID Authentication - C# ASP.NET: Getting Started: Welcome: onyangofred@gmail.com Session ID: AItOawkX3Tf3UbcZI0FvrmPlEIeY6WKjyLU0c54 The above is the result after running a simple aspx pa...

Monday, June 11, 2012

Getting Started

Welcome: onyangofred@gmail.com
Session ID: AItOawkX3Tf3UbcZI0FvrmPlEIeY6WKjyLU0c54


The above is the result after running a simple aspx page with very minimal coding. It gets your login credentials as supplied to google.

So, what is OpenID authentication?
This is a technique that allows you to share your login credentials across  multiple internet sites without having to maintain a different username and password for each of the sites. Example, you can log into your g-mail account/facebook/yahoo/blogger sites/ and then share the credentials supplied across thousands of sites. Once you logout of one site, it automatically logs you out of all the other sites. It's more secure and easy to maintain, eg change your password once, and it's replicated across multiple other sites, say you have 3 million sites you regularly use on a DAILY BASIS. instead of changing your password 3 million times, you get to update all of them using a single change in the MASTER site. That in a nutshell is a dummy's definition of OpenID Authentication. You may also be interested in OpenAuth (Authorization equivalent of OpenID). 

Now, get ready for HAPPY CODING, as it's time to get into a dummy's implementation of OpenID Authentication. Follow the following simple steps:
Open visual studio 2010 or any version you have
New aspx project
Add reference to "DotNetOpenAuth" class library (GOOGLE TO DOWNLOAD IT)
That's it, you're ready to start coding


//on the code for 1 of your pages, copy paste the code below:
//*****************************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.RelyingParty;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        OpenIdRelyingParty rp = new OpenIdRelyingParty();
        var r = rp.GetResponse();


        if (r != null)
        {
            switch (r.Status)
            {
                case AuthenticationStatus.Authenticated:
                    NotLoggedIn.Visible = false;
                    Session["GoogleIdentifier"] = r.ClaimedIdentifier.ToString();
                    Session["GoogleOther"] = r.Provider.Uri.ToString();
                   
                     var fetch = r.GetExtension<FetchResponse>();
                     if (fetch != null)
                        Session["GoogleEmail"] = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                     

//*******************NOTE: USE A VALID URL/PAGE************************
                    Response.Redirect("My Main Page Here.aspx"); //redirect to main page of your website 

//*******************NOTE: USE A VALID URL/PAGE************************
                    break;
                case AuthenticationStatus.Canceled:
                    lblAlertMsg.Text = "Cancelled.";
                    break;
                case AuthenticationStatus.Failed:
                    lblAlertMsg.Text = "Login Failed.";
                    break;
            }
        }
        else

{
        string discoveryUri = "https://www.google.com/accounts/o8/id";
        OpenIdRelyingParty openid = new OpenIdRelyingParty(); 
        var b = new UriBuilder(Request.Url) { Query = "" }; 
        var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri);

        var fetch = new FetchRequest();
        fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email, true));
        //fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.FullName, true));
        //fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.BirthDate.WholeBirthDate, true));

        req.AddExtension(fetch);

        req.RedirectToProvider();
}


    }

}
//*****************************************************

You're done. Run your page to test it.
NB: Remember to redirect the page to a different page, as thats where google (OUR EXAMPLE) will redirect you to, so you just dont want to create that endless loop on a single page.