Friday, December 23, 2011

Nunit 2.5.2 dose not work with Visual Studio 2010 (i.e. with framework 4.0


Hi All

As we all know Nunit 2.5.2 dose not work with Visual Studio 2010 (i.e. with framework 4.0). By updating appropriate ‘config’ files of Nunit we can make it run for Visual Studio 2010.

Please follow the below steps:

1.    Go to flowing folder : C:\Program Files\NUnit 2.5.2\bin\net-2.0\
2.    Open the file ‘nunit.exe.config’ and/Or  ‘nunit-agent.exe.config’ and update it with the below attributes

Under <configuration> add:
<startup>
  <supportedRuntime version="v4.0.30319" />
</startup>

And under <runtime> add:
<loadFromRemoteSources enabled="true" />

Enjoy J

Thursday, December 22, 2011

ASP.Net DataBound Controls and their Differences

1. GridView
Layouts Has default layout and support custom layout
Events Supported 1. Insert
2. Update
3. Delete
4. Filter
5. Sort
(Renders Multiple records at time)
Additional Information Supports default as well as custom paging. Allows adding custom column and apply custom formatting to columns. Supports Data bound filed, DataControl filed, Command filed and Hyperlink filed.
2. DetailsView
Layouts Has default layout and support custom layout
Events Supported 1. Insert
2. Update
3. Delete
(Renders Single records at a time)
Additional Information Supports default as well as custom paging if underlying data source supports paging. Supports Databound filed, DataControl filed, Command filed and Hyperlink filed.
3. FormView
Layouts Has no default layout and Need to set the custom layout
Events Supported 1. Insert
2. Update
3. Delete
(Renders Single records at a time)
Additional Information Supports default as well as custom paging if underlying data source supports paging. We can also customize the display format of FormView control by using style properties like EditRowStyle, EmptyDataRowStyle, FooterStyle, HeaderStyle, InsertRoStyle and PageStyle
4. Repeater
Layouts The Repeater control dose not have built in rendering of its own, which means we have to provide the layout for the repeater control by creating the Templates
Events Supported Read-only Data Presentation 
Additional Information Repeater control contains Header Template, ItemTemplete and Footer Template. Repeater control dose not support Paging, Insertion,  Selection, Editing, Deleting as this is Read-only data display control
5. DataListView
Layouts Has default layout and support custom layout
Events Supported 1. Insert
2. Update
3. Delete
(Renders Multiple records at a time)
Additional Information DataListView control contains Header Template, Alternate Item Template, ItemTemplete and Footer Template. DataListView dose not support Sorting and Paging but dose support Selection and Editing.
6. ListView
Layouts Has no default layout and Need to set the custom layout
Events Supported 1. Insert
2. Update
3. Delete
4. Filter
5. Sort
(Renders Multiple records at a time)
Additional Information To show the data in the ListView Control we need to use the LayoutTmplete, ItemTempltete.
  

Hope this helps to understand the basic difference between the ASP.Net DataBound Controls

Check If URL Exists in ASP.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;

namespace MyHttpHandler
{
public class URLChecker
{
protected bool DoseUrlExits(String UrlToCheck)
{
WebRequest req = WebRequest.Create(UrlToCheck);
WebResponse res;

bool IfUrlExitsFlag = false;
try
{
res = req.GetResponse();
IfUrlExitsFlag = true;
}
catch (Exception)
{
IfUrlExitsFlag = false;
}

return IfUrlExitsFlag;
}
}
}

Tuesday, December 20, 2011

Forms Authentication Configuration

Forms Authentication Configuration

The default attribute values for forms authentication are shown in the following configuration-file fragment.

The default attribute values are described below:

1. loginUrl points to your application's custom logon page. You should place the logon page in a folder that requires Secure Sockets Layer (SSL). This helps ensure the integrity of the credentials when they are passed from the browser to the Web server.
2. protection is set to All to specify privacy and integrity for the forms authentication ticket. This causes the authentication ticket to be encrypted using the algorithm specified on the machineKey element, and to be signed using the hashing algorithm that is also specified on the machineKey element.
3. timeout is used to specify a limited lifetime for the forms authentication session. The default value is 30 minutes. If a persistent forms authentication cookie is issued, the timeout attribute is also used to set the lifetime of the persistent cookie.
4. name and path are set to the values defined in the application's configuration file.
5. requireSSL is set to false. This configuration means that authentication cookies can be transmitted over channels that are not SSL-encrypted. If you are concerned about session hijacking, you should consider setting requireSSL to true.
6. slidingExpiration is set to true to enforce a sliding session lifetime. This means that the session timeout is periodically reset as long as a user stays active on the site.
7. defaultUrl is set to the Default.aspx page for the application.
8. cookieless is set to UseDeviceProfile to specify that the application use cookies for all browsers that support cookies. If a browser that does not support cookies accesses the site, then forms authentication packages the authentication ticket on the URL.
8. enableCrossAppRedirects is set to false to indicate that forms authentication does not support automatic processing of tickets that are passed between applications on the query string or as part of a form POST.

Original Source : http://msdn.microsoft.com/en-us/library/ff647070.aspx

Forms Authentication - Creating the Forms Authentication Cookie

We can explicitly create the Authentication Cookie in Forms Authentication mode. using below code.

E.g.

protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
{
if (Membership.ValidateUser(LoginUser.UserName, LoginUser.Password))
{
if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName,false);
}
else
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
LoginUser.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30), // value of time out property
false, // Value of IsPersistent property
String.Empty,
FormsAuthentication.FormsCookiePath);

string encryptedTicket = FormsAuthentication.Encrypt(ticket);

HttpCookie authCookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket);

Response.Cookies.Add(authCookie);

Response.Redirect("~/Default.aspx", false);
}

}
else
{
e.Authenticated = false;
}
}

Forms Authentication - Passing logged in user details to current context

We can set the Logged in user details to current context using Application_AuthenticateRequest function in Global.asax file.

E.g.

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// look if any security information exists for this request
if (HttpContext.Current.User != null)
{
// see if this user is authenticated, any authenticated cookie (ticket) exists for this user
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// see if the authentication is done using FormsAuthentication
if (HttpContext.Current.User.Identity is FormsIdentity)
{
// Get the roles stored for this request from the ticket
// get the identity of the user
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
// get the forms authetication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
// get the roles stored as UserData into the ticket
string[] roles = ticket.UserData.Split(',');
// create generic principal and assign it to the current request
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}

Check Session for expiry in Gloab.asax

One way to check, if session is expired or not is checking for cookies created in current context when session was created. We can check this in Session_Start function in Global.asax file.

E.g.

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
HttpContext context = HttpContext.Current;
HttpCookieCollection cookies = context.Request.Cookies;

if (cookies["starttime"] == null)
{
HttpCookie kookie = new HttpCookie("starttime", DateTime.Now.ToString());
kookie.Path = "/";
context.Response.Cookies.Add(kookie);
}
else
{
FormsAuthentication.SignOut();
context.Response.Redirect("Expired.aspx", false);
}
}

Read Connection String from Web.Config

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Demo
{
public class ApplicationGenral
{
internal string ConnectionStr()
{
System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/DemoApp");
System.Configuration.ConnectionStringSettings connString;
if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
{
connString = rootWebConfig.ConnectionStrings.ConnectionStrings["AuthDemo"];
if (connString != null)
return connString.ConnectionString;
else
return "";
}
else
{
return "";
}
}
}
}