Tuesday, June 30, 2009

Access image through url and render as jpeg in asp.net

class ImageProcessingModule : IHttpModule
{
public void Init(System.Web.HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
}
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app;
string requestUrl;

app = sender as HttpApplication;
requestUrl = app.Request.Url.ToString();
//requestUrl=requestUrl.Replace("https://", "http://");


//if (!requestUrl.ToLower().Contains("products")) { return; }
string[] strUrl = requestUrl.Split('/');
string ProductCode = string.Empty;
string Size = string.Empty;

for (int i = 0; i < strUrl.Length; i++)
{
if (strUrl[i].ToLower() == "images" && strUrl[i + 1].ToLower() == "products")
{
if ((i + 2) < strUrl.Length)
ProductCode = strUrl[i + 2];
if ((i + 3) < strUrl.Length)
Size = strUrl[i + 3];

string serverDirectory = app.Server.MapPath("~/UploadImages/" + Size);
string FilePath = System.IO.Path.Combine(serverDirectory, ProductCode) + ".jpg";
//app.Server.Transfer("~/ImageSystem/RetreiveImages.aspx?productcode=" + ProductCode + "&size=" + Size + "",false);
// app.Response.Redirect ("~/UploadImages/" + Size + "/" + ProductCode + ".jpg");

byte[] photo = null;
if (System.IO.File.Exists(FilePath))
{
photo = ReadImage(app.Server.MapPath( "~/UploadImages/" + Size + "/" + ProductCode + ".jpg"));//FilePath;
}
else
{
photo = ReadImage(app.Server.MapPath("~/UploadImages/" + Size + "/Missing.jpg"));//FilePath;

}
if (photo != null)
{
app.Response.ContentType = "image/jpeg";
app.Response.Cache.SetCacheability(HttpCacheability.Public);
app.Response.BufferOutput = false;
app.Response.OutputStream.Write(photo, 0, photo.Length);
}






}
}
// app.Response.Write(ProductCode+" hi "+ Size);

}

public void Dispose()
{
}
private static byte[] ReadImage(string p_postedImageFileName)
{
// bool isValidFileType = false;
try
{
FileInfo file = new FileInfo(p_postedImageFileName);


FileStream fs = new FileStream(p_postedImageFileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] image = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return image;

// return null;
}
catch (Exception ex)
{
return null;
//throw ex;
}
}
}

Web.Config settings
register http module



Thursday, May 28, 2009

Integrating BlogEngine into an existing website











Introduction to Blog Engine.Net -






BlogEngine.NET is open source .BlogEngine.NET is a full-featured blogging platform that is a breeze to set up, customize, and use. BlogEngine.NET works with your choice of data source; you may use SQL Server, or you may take the plug'n'play approach using XML files.

















  • Plug 'n' Play
    BlogEngine.NET is very easy to setup and customize. To make it work, just upload the files to an ASP.NET 2.0 webserver and you're ready to start writing. No database configuration, just plug'n'play.





  • Full Featured
    BlogEngine.NET comes with all the features you would expect from a modern blog engine as well as new unique features such as AJAX comments and screenshot trackbacks.





  • Web 2.0
    BlogEngine.NET features social bookmarks, OpenSearch support, XFN tags, AJAX, Microsummaries, Gravatars, coComments, tag cloud, Google sitemap and other so-called Web 2.0 features.





  • Cool Themes
    BlogEngine.NET comes with some very cool themes for you to choose from. If you want to modify or create a new theme you can do so easily with just a basic understanding of HTML and CSS.





  • XHTML Compliance
    All the controls in BlogEngine.NET are 100% XHTML 1.0 compliant. All posts you write automatically becomes compliant thanks to the tinyMCE text editor.





  • Extendable
    BlogEngine.NET is built from the ground up using nothing but C# and ASP.NET 2.0 all with simplicity in mind. It means that you can write new controls and themes by using the skills you already have as a .NET developer.





Above information about BlogEngine I have picked from http://www.asp.net/ website.I don't want to describe more about information regarding blog my concern only how to integrate it to existing website.






BlogeEngine support three kinds of membership providers-






We have to change setting in web.config according to your requirement .First of all change setting in custom section blogprovider .






<configSections>






<sectionGroup
name="BlogEngine">






<section
name="blogProvider"
requirePermission="false"
type="BlogEngine.Core.Providers.BlogProviderSection, BlogEngine.Core"
allowDefinition="MachineToApplication"
restartOnExternalChanges="true"/>






</sectionGroup>






</configSections>






<BlogEngine>






<blogProvider
defaultProvider="DbBlogProvider">






<providers>






<!--<add name="XmlBlogProvider" type="BlogEngine.Core.Providers.XmlBlogProvider, BlogEngine.Core"/>-->






<add
name="DbBlogProvider"
type="BlogEngine.Core.Providers.DbBlogProvider, BlogEngine.Core"
connectionStringName="BlogEngine" />






</providers>






</blogProvider>






</BlogEngine>






















  1. XmlMembershipProvider: - All data store in App_Data directory of Asp.net .so full rights must be applied on this folder.





    Web.Config setting –




































<membership
defaultProvider=" XmlMembershipProvider ">






<providers>






<clear/>






<add
name="XmlMembershipProvider"
type="BlogEngine.Core.Providers.XmlMembershipProvider, BlogEngine.Core"
description="XML membership provider"
passwordFormat="Hashed"/>












</providers>






</membership>












<roleManager
defaultProvider=" XmlRoleProvider"
enabled="true"
cacheRolesInCookie="true"
cookieName=".BLOGENGINEROLES">






<providers>






<clear/>






<add
name="XmlRoleProvider"
type="BlogEngine.Core.Providers.XmlRoleProvider, BlogEngine.Core"
description="XML role provider"/>












</providers>






</roleManager>






















  1. SqlMembershipProvider :- If your application using AspnetMembership then this option will be more useful.











    Web.Config setting –






























<membership
defaultProvider="SqlMembershipProvider">






<providers>






<clear/>






<add
name="SqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="BlogEngine"
applicationName="BlogEngine"/>






</providers>






</membership>












<roleManager
defaultProvider="SqlRoleProvider"
enabled="true"
cacheRolesInCookie="true"
cookieName=".BLOGENGINEROLES">






<providers>






<clear/>






<add
name="SqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="BlogEngine"
applicationName="BlogEngine"/>






</providers>






</roleManager>






















  1. DbMembershipProvider : - If your website using your on authentication using rather than aspnetmembership then this option will be best.











    Web.config settings :












<membership
defaultProvider="DbMembershipProvider">






<providers>






<clear/>






<add
name="DbMembershipProvider"
type="BlogEngine.Core.Providers.DbMembershipProvider, BlogEngine.Core"
passwordFormat="Clear"
connectionStringName="BlogEngine"/>






</providers>






</membership>












<roleManager
defaultProvider="DbRoleProvider"
enabled="true"
cacheRolesInCookie="true"
cookieName=".BLOGENGINEROLES">






<providers>






<clear/>






<add
name="DbRoleProvider"
type="BlogEngine.Core.Providers.DbRoleProvider, BlogEngine.Core"
connectionStringName="BlogEngine"/>






</providers>






</roleManager>












All of three them I want to describe DbMembershipProvider because other two no need to describe more here. lot of websites using own authentication mechanism so DbMembershipProvider will be best way to integration in my opinion.






Following changes should be made in BlogEngine.Core assembly:-






DB Provider classes are shown in following diagram:-































  1. Change table prefix in DbMembershipProvider.cs . Either set your table prefix or blank



































  1. Change table name and field name according to your database in DbMembershipProvider.cs class
























  2. Change table prefix , table name and field name according to your database in DbMembershipProvider.cs class





























  3. Define roles according to your database entries in web.sitemap


















    Summary:- BlogEngine.Net is best open source blog application that provide very simple integration providers






    We can also implement DBMebershipProvider for custom provider in our application.






Tuesday, April 14, 2009

Easy steps to create an asp.net website

Following tricks which have help to develop an asp.net website in quick way-
1.Form Authentication:
This is a cookie based authentication system where the username and passport is stored in a text file or a database. We will be focusing on this authentication model in this tutorial.
WEB.CONFIG







FormsAuthentication.AuthenticateThe single login button on the webpage calls the Login_Click method when clicked. In this method, we use the FormsAuthentication.Authenticate(username,password) to get ASP.NET to check the credentials of the user. The parameters for this method is pretty straightforward and it just returns a boolean value. FormsAuthentication.RedirectFromLoginPageIf the user is providing proper credentials, then we'll use the FormsAuthentication.RedirectFromLoginPage method. The parameters of this method are a username string and a boolean value. The first parameter is a username string and it is a name of the user for cookie authentication purposes. The value you put in there will be the user name that the client is associated with. It does not need to match the username used in the FormsAuthentication.Authenticate method but it is advisable to set the cookie to the username that was used to log in. The second parameter is a boolean value and it specifies whether or not a durable cookie (one that is saved across browser sessions) should be issued.If you remember, when a user requests a page without proper authentication, they are redirected to the login page. After setting the cookie, the RedirectFromLoginPage will then send the user back to the page they came from.
2.Use Free Tempalates:
Lot of web sites provide us free tempalte for diffrent layout.We can use easily CSS,HTML for our website.
3.Use Log4Net: Log4net is an open source library that allows .NET applications to log statements to a variety of targets.
4.Use Website Starterkits:
To find out starterkit following links should be useful-
http://www.asp.net/community/projects/