ASP.Net
ASP.Net
NET
Introduction :
This article explains how ASP.NET Web Form Controls and HTML Serer Control can
be made to exec!te client side script be"ore postin# the pa#e bac$ to the serer.
Here % hae ta$en an example o" a b!tton& which when clic$ed calls a "!nction
'alidate() which is on the client side "irst and then s!bmits the pa#e.
ASP.NET Web Form Control
Let !s start with a b!tton which is a Asp.NET Web Form Control t*pe
i.e +asp,b!tton id-.accept/!tton. r!nat-.serer. Text-.S!bmit. +0asp,b!tton1
%n the Pa#e2Load Eent o" this pa#e write the "ollowin# statement ,
acceptButton.Attributes.Add(onclic!" return Validate(##
Here 'alidate() is a simple "!nction written in 3aaScript or '/Script. %n this "!nction
all client side alidations can be done and when all the alidation chec$s are met
then the "!nction merel* ret!rns tr!e to post the pa#e bac$ to the serer. 4et!rnin#
"alse "rom the 'alidate() "!nction does not post the pa#e.
$T%& Ser'er Control
These controls are similar to the HTML control& Howeer the* hae an attrib!te r!nat
- .serer.& which ma$es them serer controls.
+%NP5T id-.accept/!tton. onclic$-.ret!rn alidate()6. t*pe-.b!tton.
al!e-.S!bmit. name-.m*/!tton. r!nat-.serer.
onsererclic$-.m*/!tton2SererClic$.1
Here we see that this control has two attrib!tes onClic! which calls the client side
"!nction 'alidate and onSer'erClic! which calls the serer side "!nction. Here a#ain
we hae the same alidation "!nction& which ret!rns a boolean. 4et!rnin# tr!e
ca!ses the pa#e to be posted to the serer and exec!te the m*/!tton2SererClic$()
on the serer.
Pa(elet Controls
Pa(elet Controls also $nown as )ser Controls& are nothin# b!t Web Forms which
are bein# modi"ied to be !sed as a Ser'er Control. %n preio!s ersion(ASP 7.8) we
!sed incl!de "iles tho!#h not the best sol!tion "or res!abilit*& b!t ASP.Net proides
with power"!l )ser Controls called as Pa(elet Controls&these *or! similar to a
Ser'er Control. Pa#elet controls can be !sed in more than one hostin# pa#e and
can be !sed more than once in the same hostin# pa#e.Pa(elet controls +a'e t+e
e,tension -.asc, .9o! cannot r!n a Pa#elet Control& *o! need a hostin# pa#e to
!se this Control& b!t to !se this control in hostin# pa#e *o! hae to re(ister it
usin( t+e ./0e(ister ...... /1 directi'e.Commonl* Pa#elet controls co!ld be
!sed as header or "ooter "or the Pa#e.
In t+e E,ample belo* $eader.asc, is t+e Pa(e&et Control" *+ile t+e +ostin(
Pa(e is $eader.asp,.
Note: Header.ascx(Pa#elet control) has been re#istered !sin# the re#ister ta#& also
not the !sa#e o" Ta#pre"ix&Ta#name and the src attrib!tes.
$eader.asc,
+script lan#!a#e-.b. r!nat-.serer. 1
p!blic propert* te,tBo,Value as strin#
#et
ret!rn m*text.text
end #et
set
m*text.text - al!e
end set
end propert*
+0script1
+center1
+br1
+asp,label id-.m*lbl. text-.TH%S %S THE HEA:E4 PA;ELET C<NT4<L.
borderst*le-= "orecolor-.red. r!nat-.serer. 01
+br1+br1
+asp,textbox id-.m*text. r!nat-.serer. 01
+br1
+0center1
$eader.asp,
+>? re#ister ta(pre2i,3Pa(e&et Ta(Name3$eader src3$eader.asc,
>1
+"orm r!nat-.serer.1
.pa(elet:+eader id3m4Pa(elet te,tbo,'alue3pa(elet control attribute
runat3ser'er 51
+br1
+br1
+asp,b!tton id-.b!tton@. text-.b!tton @. r!nat-.serer. 01
+asp,label id-.lbl. text-.label and b!tton in the aspx "ile and the Pa#elet control is
!sed here. r!nat-.serer. 01
+0"orm1
Sa'e t+ese 2ile to t+e Web Ser'er (IIS# ***root 2older and run
+ttp:55local+ost5+eader.asp,
Automatic Transaction usin( ASP.NET
Two or more .NET Framewor$ obAects can participate in the same transaction. <ne or
more .NET Framewor$ obAects can participate in the same transaction as one or
more C<MB obAects. All obAects m!st be hosted in Microso"t Windows =C Component
Serices so as to receie serices s!ch as transaction processin#.
There are seeral considerations *o! m!st address in desi#nin# transactions that
span C<MB and .NET Framewor$ obAects. For example& in order "or a .NET
Framewor$ obAect to participate in an automatic transaction& the .NET Framewor$
class m!st be re#istered with Window =C Component Serices. Howeer& not all
transactions are a!tomatic. The actiities *o! per"orm when pro#rammin#
transactions depend on the transaction model *o! choose.
Microso"t Transaction Serer (MTS)& C<MB& and the Common Lan#!a#e 4!ntime
s!pport the same a!tomatic& distrib!ted transaction model.
<nce an ASP.NET pa#e& WebMethod& or .NET Framewor$ class is mar$ed to
participate in a transaction& it will a!tomaticall* exec!te within the scope o" a
transaction. 9o! can control an obAectDs transactional behaior b* settin# a
transaction attrib!te al!e on the pa#e& WebMethod& or class. The attrib!te al!e& in
t!rn& determines the transactional behaior o" the instantiated obAect.
Th!s& based on the declared attrib!te al!e& an obAect will a!tomaticall* participate
in an existin# or on#oin# transaction& be the root o" a new transaction& or neer
participate in a transaction at all .The ASP.NET "ramewor$ s!pports a!tomatic
transactions on Windows =C . /* insertin# a transaction directie in *o!r ASP.NET
pa#e& *o! can instr!ct the pa#e to participate in an existin# transaction& be#in a new
transaction& or neer participate in a transaction.
The table below lists and describes the transaction directies aailable in ASP.NET.
Directive
6escription
NotS!pported This al!e indicates that the pa#e does not r!n within
the scope o" transactions. When a reE!est is processed&
its obAect context is created witho!t a transaction&
re#ardless o" whether there is a transaction actie.
S!pported The pa#e will r!n in the context o" an existin#
transaction& i" one exists. %" not& it will r!n witho!t a
transaction.
4eE!ired The pa#e reE!ires a transaction. %t will r!n in the
context o" an existin# transaction& i" one exists. %" not& it
will start one.
4eE!iresNew The pa#e reE!ires a transaction and a new transaction
will be started "or each reE!est.
9o! can indicate the leel o" transaction s!pport on a pa#e b* placin# the directie in
*o!r code. For example& *o! can ens!re that the pa#e actiities alwa*s exec!te in
the scope o" a transaction b* insertin# the "ollowin# directie,
+>? Pa#e Transaction-F4eE!iredF >1
The de"a!lt transaction state is None& which indicates that the transaction context
will be i#nored b* the ASP.NET "ramewor$.
There is no explicit directie "or this al!e6 in other words& *o! cannot add pa(e
Transaction37None7 to *o!r pa#e. %nstead& simpl* omit the transaction directie
to appl* the de"a!lt al!e.
Con2i(urin( Applications in ASP.Net
Applications can be con"i#!red to behae independentl* in ASP.Net !sin# the
Gweb.con"i#F "ile. The Gweb.con"i#F is an HML based con"i#!ration "ile which is !sed to
con"i#!re settin#s "or an application. The* wor$ m!ch li$e the G.iniF "iles which are
!sed in Windows except that these speci"* settin#s "or a Web application. There can
be m!ltiple Gweb.con"i#F "iles and each o" the con"i#!ration is "or the director* and
the directories below it. These "iles cannot be accessed oer the web& ie& %%S does not
allow them to be sered and bloc$s access to it m!ch li$e the #lobal.asa "ile.
Let !s loo$ a brie" loo$ at each o" the sections in the web.con"i# "ile.
AppSettin(s
For c!stom settin# "or an application.
Aut+entication
For the a!thentication schema. This is where *o! speci"* the a!thentication
mode(Windows& Forms& Passport or None) "or *o!r application. <ne can ma$e !se o"
Passport a!thentication to Gpassport enableF *o!r website hereI For more re"er to the
Microso"t Passport S:C which is aailable as a "ree download.
Aut+ori8ation
This is !sed to control client access to ari!os 54L reso!rces in the application. 9o!
can speci"* roles and !sers in this section allowin# or disallowin# them access to
speci"ic reso!rces.
Bro*serCaps
This is !sed to detect and control the browser capabilities.
Compilation
This section is !sed to control the behaio!r o" the compilation process "or the
ASP.Net application. 9o! can control ario!s options li$e de"a!lt scriptin# lan#!a#e&
deb!##in# mode and so on.
CustomErrors
This is !sed to speci"* a c!stom error messa#e "or a partic!lar HTTP stat!s code.
This can be directl* related to the c!stom errors tab o" a web site properties in the
%%S serer.
9lobali8ation
This is !sed to speci"* the #lobalisation properties o" the application li$e encdoin#
t*pes(5TF) and so on.
$ttp$andlers
This is where one can speci"* how the reE!est "or an speci"ic reso!rce has to be
handled. This wor$s m!ch li$e the %SAP% "ilter handler in the web site properties. For
example& one can write a handler which seres !p a .alx "ile and map the .alx
extension in this section.
$ttp%odules
This section is !sed to map a HTTP mod!le to an action.
$ttp0untime
For con"i#!rin# http r!ntime settin#s.
Identit4
This is !sed to con"i#!re the identit* !nder which the application sho!ld r!n. This can
be !sed to impersonate the application to enable the application to r!n !nder a
di""erent set o" credentials.
%ac+ine:e4
This is !sed to control the decr*ption0encr*ption al#orithm "or stora#e o" "orm based
coo$ies. <ne can choose "rom M:J& SHA@ or 7:ES t*pes o" al#orithms to encr*pt the
contents o" the coo$ie data.
Pa(es
This section is !sed speci"* the pa#e leel directie "or all the pa#es in the c!rrent
application. For example& one mi#ht set the deb!# leel or the de"a!lt lan#!a#e or
enable b!""erin# "or all the pa#es in this ta#.
Process%odel
This section is !sed to speci"* how the application behaes within the ASP.Net
process. This section allows to speci"* the al!es "or wor$er threads etc. which was
possible in the preio!s ersions o" %%S !sin# the re#istr* and the metabase.
Securit4Polic4
This section is !sed to speci"* the sec!rit* polic* con"i#!ration "ile "or the c!rrent
application.
SessionState
This section is !sed to mana#e the session state "or the application. 9o! can speci"*
how session sho!ld be handled in the c!rrent application& !sin# coo$ies or coo$ieless
or een ia SKL SererI
Trace
This section is !sed to con"i#!re the trace settin#s o" the application. <ne can speci"*
how tracin# sho!ld be handled in the application and enable or disable them here.
WebSer'ices
This section is !sed to control the webserices created with the ASP.Net. <ne can
speci"* the t*pe o" protocol and the extension t*pes o" the webserice here.
As one can see "rom the aboe sections& most o" them hae a direct mappin# to the
properties that one can access "rom the web site inside the %%S con"i#!ration. The
main adanta#e o" !sin# s!ch a "ile is that it allows each diretor* within an
application to behae in a di""erent mode. As it is HML based it is also easier to
chan#e the con"i#!ration easil*. Also& %%S a!tomaticall* detects chan#es to the
Gweb.con"i#F "iles and a!tomaticall* loads the con"i#!ration settin#s when a new
reE!est comes thro!#h.
)ser Controls in ASP.NET
Introduction :
The idea behind !ser control is that *o! can create re!sable sections o" code or
content as separate ASP.NET controls& and then !se them in other pa#es witho!t
hain# to chan#e the code& or een be aware o" how it wor$s.
While the Code /ehind techniE!e is primaril* aimed at A!st inheritin# code classes
into a pa#e& !ser controls allow *o! to inherit parts o" the !ser inter"ace as well.
%n"act& the* are a wa* o" encaps!latin# other controls and code into a re!sable
pac$a#e.
%n this article& % will demonstrate how to deelop 5ser Control in a Code behind "ile. %
will C!stomiLe a /!tton& s!ch that on clic$in# it& it will displa* some data in a
data#rid. This b!tton once created can be !sed an*where thro!#ho!t the assembl*.
The /!tton has the "lexibilit* to ta$e an* Table name at r!ntime& and when clic$ed it
will displa* the contents o" that table.
We will "irst hae a .b "ile(i.e a CodeMbehind "ile) which will hae the lo#ic to connect
to the database& retriee the records o" the speci"ied table and displa* it in the
data#rid.
<!r CodeM/ehind "ile needs to incl!de all the namespaces which will be !sed b* o!r
control. Apart "rom that we need to incl!de the S4stem and S4stem.Web.)I
Namespaces. Also& o!r class sho!ld inherit "rom 5serControl. Also& we hae to
declare public instances o" the ASP.NET serer controls that we will !se in o!r
control.
)serControl.'b
%mports S*stem
%mports S*stem.Web.5%
%mports S*stem.Web.5%.WebControls
%mports S*stem.:ata
%mports S*stem.:ata.<LE:/
p!blic class M*Code/ehind
inherits 5serControl
p!blic data#rid@ as :ata;rid
p!blic displa* as b!tton
priate dbName as Strin#
priate tName as Strin#
Propert* :ata/aseName() as Strin#
;et
ret!rn dbName
end ;et
set
dbName - al!e
end set
end Propert*
Propert* TableName() as Strin#
;et
ret!rn tName
end ;et
set
tName - al!e
end set
end Propert*
s!b show(sender as obAect& e as eentar#s)
tr*
:im connectionStrin# As Strin#
connectionStrin# - .Proider - Microso"t.3et.<LE:/.N.8 65ser %d -6Password
-6:ata So!rce - . O dbName
:im m*Connection As New <le:bConnection(connectionStrin#)
:im m*Command As New <le:bCommand()
:im m*%nsertStrin# As Strin#
m*Command.Connection - m*Connection
m*Connection.<pen()
m*%nsertStrin# - .select P "rom . O tName
m*Command.CommandText - m*%nsertStrin#
data#[email protected]!rce - m*Command.Exec!te4eader()
data#[email protected]()
m*connection.close()
catch ex as exception
response.write(.Error , . O ex.messa#e)
end tr*
end s!b
end class
As *o! can see "rom the aboe code& we hae declared two properties
:atabaseName and TableName. These properties will be !sed to d*namicall*
associate a partic!lar table to the data#rid.
Note : We co!ld still enhance the "!nctionalit* and ma$e ConnectionStrin# as one o"
the propert* so that we can connect to di""erent databases d*namicall*. Howeer& "or
the present case we will connect onl* to Microso"t Access :atabase.
Next& We Create a .ascx "ile& !sin# the ?Control directie to indicate the name o" the
class the !ser control inherits and the path to the so!rce "ile we created aboe.
%ncl!de the serer controls and an* text that we want the !ser control to displa*.
When we declare the %: attrib!te on each serer control& we ma$e s!re it matches
the name o" the instance we created in the codeMbehind "ile.
For example& the %: attrib!te in the element below is :ispla*& correspondin# to the
instance ariable created in the aboe code.
uc.asc,
+>? control inherits - .M*Code/ehind. src - .5serControl.b. >1
+asp,b!tton id-.displa*. onclic$ - .show. text - .:ispla*. r!nat-.serer. 01
+asp,data#rid id-.data#rid@. r!nat - .serer.01
To !se o!r 5ser Control& we need to add it to the Web Form. For this& we need to !se
the ;0e(ister :irectie that incl!des ,
A ta(pre2i, attrib!te& which associates a pre"ix with the !ser control. This pre"ix will
be incl!ded in openin# ta# o" the !ser control element.
A ta(name attrib!te& which associates a name with the !ser control. This name will
be incl!ded in the openin# ta# o" the !ser control element.
A Src attrib!te& with de"ines the irt!al path to the !ser control "ile that *o! are
incl!din# in the Web Forms pa#e.
)sin()C.asp,
+>? re#ister ta#pre"ix - .d;rid. ta#name - .details. src-.!c.ascx. >1
+html1
+bod*1
+"orm r!nat-.serer.1
+d;rid,details id-.db#. :ata/aseName - .db.mdb. TableName - .Articles.
r!nat-.serer. 01
+0"orm1
+0bod*1
+0html1
%n the aboe .aspx "ile& we see how o!r !ser control is !sed. We set .db.mdb. as the
database and .Articles. as the Table. We can !se the same !ser control in an* other
"orm and set some other database and Table to #et the correspondin# res!lt in a
data#rid.
Cr4stal 0eports in Visual Studio .NET
Cr*stal 4eports in 'is!al St!dio .NET extends the power"!l reportin# capabilit* o"
Cr*stal 4eports on Microso"t .NET plat"orm. 9o! can !se Cr*stal 4eport :esi#ner in
'is!al St!dio .NET to create a new report or modi"* an existin# report. 9o! can $eep
the 4eport on the local machine or p!blish it as a 4eport Web Serice on the Web
Serer. :ependin# on whether *o! are deelopin# a Windows or Web Application&
*o! can then host the report with either the windows "orm iewer or the web "orm
iewer& respectiel*.
T+is article e,plains :
@. Creatin# a Cr*stal 4eport
=. Addin# an Existin# Cr*stal 4eport
7. Hostin# and 'iewin# a Cr*stal 4eport !sin# the Windows Form 'iewer
<. Creatin( a Cr4stal 0eport
%n #eneral& we can create a Cr*stal 4eport as part o" 'is!al CQ& Mana#ed CBB
Application o" '/ .NET proAects. %n this wal$thro!#h& we will be creatin# reports in
'is!al CQ proAects.
We will !se the Northwind database o" the Microso"t Access "or demonstration
p!rpose.
Followin# is the step b* step proced!re ,
@. %n 'is!al St!dio .NET& select File R New R ProAect. The New ProAect dialo# box
appears& here select 'is!al CQ ProAects in the le"t pane and select Windows
Application in the ri#ht pane. ;ie the name .:emo. to the proAect and select
the location as C, (Note , An* location can be selected "or the proAect)& Clic$
<C.
This creates a 'is!al CQ proAect& :emo& in C,S:emo
=. %n the 'is!al St!dio Sol!tion Explorer& ri#ht clic$ on the proAect :emo. Select
Add R Add New %tem "rom the men!. The Add New %tem M :emo dialo# box
appears.
7. %n the Add New %tem M :emo dialo# box& !nder Cate#ories& with Local ProAect
%tems hi#hli#hted in the le"t pane& select Cr*stal 4eport in the ri#ht pane.
Enter .C!stomer4eport.rpt. in the Name "ield. Clic$ the <pen b!tton. This
ino$es the Cr*stal 4eport :esi#ner that will help *o! create and desi#n
C!stomer4eport.rpt.
6esi(nin( a Cr4stal 0eport : The Cr*stal 4eport :esi#ner in 'is!al St!dio .NET
closel* resembles the :esi#ner o" the 4eport :esi#ner Component that r!ns on
'is!al /asic T.8.
When *o! create a report& the Cr*stal 4eport :esi#ner opens with the Cr*stal 4eport
;aller* that o""ers the "ollowin# options ,
5se a 4eport Expert to #!ide *o! thro!#h the report creation process.
<pen a blan$ report.
Create a new 4eport "rom an existin# report.
For the p!rpose o" this example& we will !se the Standard 4eport Expert to create a
report titled C!stomer4eport. The C!stomer4eport.rpt "ile will access the database
ia A:< and displa* the res!lt.
@. %n the Cr*stal 4eport ;aller*& !nder Create a New Cr*stal 4eport :oc!ment&
select 5sin# the 4eport Expert. 5nder Choose an Expert& select Standard&
Clic$ <C. This ino$es the Standard 4eport Expert.
=. %n the :ata tab o" the Standard 4eport Expert& do!bleMclic$ <LE :/ (A:<).
This ino$es the <LE :/ (A:<) dialo# box.
7. %n the <LE :/ (A:<) dialo# box& select Microso"t 3et N.8 <LE :/ Proider.
Clic$ Next.
N. Clic$ the sE!are b!tton adAacent to the entr* "ield "or :atabase Name.
J. %n the <pen dialo# box& select the NorthWind database !nder the appropriate
path. Clic$ <pen
T. %n the <LE :/ (A:<) dialo# box& accept Access as the :atabase T*pe. Enter
the appropriate 5ser %:. Clic$ Next.
U. Accept the de"a!lt adanced in"ormation& and clic$ Finish.
V. The Standard 4eport Expert shows the new connection to the database& with
the tables and "ields in the database. Expand Tables& select the C!stomer
Table and clic$ %nsert Table.
W. %n the lin$s tab& clic$ Next.
@8. %n the Fields Tab& add the "ields reE!ired. Clic$ Next.
There are some other tabs also& to enhance the "!nctionalit*. For example *o!
can create the ;ro!p wise report !sin# the ;ro!p tab& or *o! can incl!de
#raphs !sin# the Chart Tab etc. For o!r demo the aboe incl!sions are
s!""icient.
@@. To sae the C!stomer4eport.rpt with :emo& ri#ht clic$ on the proAect :emo in
the Sol!tion Explorer and select Sae :emo "rom the shortc!t men!
@=. Select File R Close Sol!tion to close the sol!tion.
=. Addin( an E,istin( Cr4stal 0eport
%n #eneral& we can add an existin# Cr*stal report to a 'is!al CQ& mana#ed CBB or
'is!al /asic proAect in 'is!al /asic .NET. %n this wal$thro!#h& we will be addin#
existin# reports to 'is!al CQ proAects.
The "ollowin# scenario ill!strates how to add an existin# Cr*stal 4eport&
Emplo*ees4eport.rpt to the 'is!al CQ proAect .:emo.& which has been created in the
preio!s section .Creatin# a Cr*stal 4eport.
@. %n 'is!al St!dio .NET select File R <pen R ProAect to open .:emo.
=. %n the 'is!al St!dio Sol!tion Explorer& ri#ht clic$ the 'is!al CQ proAect
.:emo.. Select Add R Add Existin# %tem "rom the shortc!t men!. This ino$es
the Add R Existin# %tem M :emo dialo# box.
7. %n the Add Existin# %tem M :emo dialo# box& speci"* the location and name o"
the report to be added. %n this example& we are addin# the sample report
Emplo*ees4eport.rpt.
Emplo*ees4eport.rpt will appear as an item !nder the :emo proAect in the Sol!tion
Explorer. Sae the proAect.
>. $ostin( and Vie*in( a Cr4stal 0eport usin( t+e Windo*s Forms Vie*er
%n the wal$thro!#h below& we will create a 'is!al CQ Windows application to host a
Cr*stal 4eport with a Windows Forms 'iewer. The Cr*stal 4eport we will host is
C!stomer4eport.rpt that has been created in the section .Creatin# Cr*stal 4eport.
aboe.
@. %n 'is!al St!dio .NET& select File R New R ProAect. This ino$es the New ProAect
dialo# box. Here select 'is!al CQ proAects in the le"t pane and Windows
Application in the ri#ht pane. Name the proAect as 'iew4eports and select
location as C,S. Clic$ <C. The sol!tion will be created in C,S'iew4eports
=. 5pon clic$in# <C in the New ProAect dialo#& 'is!al St!dio creates a Window
Form (with the de"a!lt name [email protected].) in the proAect. 'is!al St!dio opens
the Windows Form in its :esi#n 'iew. The 'is!al St!dio Toolbox also displa*s
a set o" controls "or the Windows Form. Select the Cr*stal4eport'iewer "rom
the Toolbox.
The Windows Forms 'iewer is now aailable as a control with the "ollowin#
components ,
Toolbar containin# these b!ttons ,
o #o to "irst pa#e
o pa#e bac$ward
o pa#e "orward
o #o to last pa#e
o #o to speci"ied pa#e
o close c!rrent window ("or #ro!p or s!breport onl*)
o print
o re"resh report
o export
o to##le #ro!p tree
o ma#ni"*0red!ce report
o search
;ro!p Tree
Main 4eport Window
Speci24in( a 0eport to be +osted on Windo*s Form Vie*er : 9o! can speci"*
a report to be hosted on the Windows Forms 'iewer thro!#h the 4eportSo!rce
propert* o" the Cr*stal4eport'iewer <bAect.
%n the desi#n iew& select the Cr*stal4eport'iewer <bAect and in the Propert*
Window in the 4eportSo!rce Propert*& browse to c,SdemoSC!stomer4eport.rpt.
A"ter b!ildin#& r!n the Windows Application& the Windows Forms 'iewer will displa*
C!stomer4eport.rpt
W$AT?S S@ C@%%@N AB@)T C@%%@% TAPE SASTE% (CTS#
The wide spread o" the technolo#ies which Microso"t .Net deliers is astonishin# and
asto!ndin# as it proides a wa* to $noc$ down the barriers that hae traditionall*
diided deelopers into separate b!t !neE!al lan#!a#e worlds. The combination o" all
these technolo#ies alread* interestin# on its own& are more compellin# and nicer now
with the adent o" !ni"ied s*stem called as CTS.
This article introd!ces *o! with the common $nowhow abo!t the common t*pe
s*stem and the need o" it to be there "or !s. This article hi#hli#hts the "ollowin#
parameters,
%NT4< T< THE C<MM<N T9PE S9STEM
%NT4< T< THE C<MM<N LAN;5A;E 45NT%ME
WH9 :< WE NEE: A C<MM<N 45NT%ME AN: T9PE S9STEM X
C<MM<N LAN;5A;E SPEC%F%CAT%<N
C<MM<N METH<:S <F CTS
SET <F LAN;5A;ES THAT TA4;ET THE CL4
CTS /ENEF%TS
INT0@ T@ C@%%@N TAPE SASTE%:
The CTS proides eer* lan#!a#e r!nnin# on the .NET plat"orm with a base
set o" t*pes. While the CTS is responsible "or de"inin# the t*pes that can be
!sed across the .Net lan#!a#es& most lan#!a#es choose to implement aliases
to those t*pes. For example& a "o!rMb*te inte#er al!e is represented b* the
CTS t*pe S*stem.%nt7=.and CQ then de"ines an alias "or this called int.
Eer* thin# in the CTS is an obAect. %n"act not onl* is eer*thin# an obAect b!t
een more importantl*& all obAects implicitl* derie "rom a sin#le base class
de"ined as a part o" the CTS. This base class is called s*stem.obAect. The
desi#ners o" CTS were "aced with the tas$ o" creatin# a t*pe s*stem in which
eer* thin# is an obAect b!t the t*pe s*stem wor$s in an e""icient manner &
when applicable. Their sol!tion was to separate the CTS t*pes into two
cate#ories,
@. 'AL5E T9PES ( assi#nin# act!al al!es to the ariables)
=. 4EFE4ENCE T9PES ( re"erencin# to point at the address o" the obAect)
INT0@ T@ T$E C@%%@N &AN9)A9E 0)NTI%E:
The hi#hMper"ormance CL4 (Common Lan#!a#e 4!ntime) incl!des an
o Exec!tion en#ine&
o A #arba#e collector&
o 3!stMinMtime (3%T) compiler&
o A sec!rit* s*stem&
o A rich class "ramewor$ (The .Net "ramewor$).
The CL4 was desi#ned to s!pport m!ltiple lan#!a#es. The CL4 is responsible
"or mana#in# the exec!tion o" code compiled "or the .Net plat"orm. Code
reE!irin# the CL4 at r!ntime in order to exec!te is re"erred to as . mana#ed
code.. Compilers that tar#et the .NET plat"orm #enerate mana#ed code that
relies on a core set o" serices proided b* the CL4. :espite the "act that
mana#ed code is compiled on an intermediate lan#!a#e&.Net pro#rams are
not interpreted b* a .irt!al machine.. The CL4 is responsible "or compilin#
.Net applications to natie machine code. %t is also technicall* possible to port
the CL4 to a ariet* o" hardware plat"orms& een other pro#rammin# s*stems.
Some o" the salient "eat!res o" CL4 incl!de,
o CrossMlan#!a#e inte#ration
o Enhanced sec!rit*
o 'ersionin# and deplo*ment s!pport
o :eb!##in# and pro"ilin# serices
o Memor* mana#ement
W$A 6@ WE NEE6 A C@%%@N 0)NTI%E AN6 TAPE SASTE% B
Each pro#rammin# lan#!a#e seems to brin# its own datat*pes with it. For
example& '/ represents strin#s !sin# the /ST4 str!ct& CBB o""ers char and
wchar datat*pes& and MFC o""ers the CStrin# class. The CBB int datat*pe is a
7=Mbit al!e where the '/ inte#er datat*pe& prior to '/.NET& is a @TMbit al!e.
The primar* #oal o" the .NET plat"orm is to ma$e it easier "or the deeloper to
write applications. C<M deelopers are exposed to a headache as the* need
to be concerned abo!t ;5%:s& H4ES5LTs& earl* ers!s Late bindin#&
mana#in# re"erence co!nts& the t*pe o" threadin# model *o!r component will
r!n in& and prox* and st!b la*ers etc.
Fort!natel* some o" the pro#rammin# enironment and "ramewor$s s!ch as
'/ and ATL come to the resc!e and red!ces m!ch o" this complexit*.For
example& '/ a!tomaticall* #enerates the appropriate ;5%:S and also
mana#es re"erence co!nts "or C<M obAect !sed within the application. /!t still
man* deelopers will ar#!e that 'is!al /asicDs implementation o" C<M leaes
a lot to be desired. The sol!tion has been presented in the "orm o" creatin#
s!ch a s*stem which is common to all the lan#!a#es related to this plat"orm
and so the common t*pe s*stem was created.
C@%%@N &AN9)A9E SPECIFICATI@N:
The Common lan#!a#e speci"ication describes a common leel o" lan#!a#e
"!nctionalit*. The CLS is a set o" r!les that a lan#!a#e compiler m!st adhere
to in order to create .Net applications that r!n in the CL4. An*one who wants
to write a .NetMcompliant compiler needs simpl* to adhere to these r!les and
thatDs it. The relatiel* hi#h minim!m bar o" the CLS enables the creation o" a
cl!b o" CLSMcompliant lan#!a#es. Each member o" this cl!b enAo*s d!al
bene"its, complete access to .Net "ramewor$ "!nctionalit* and rich
interoperabilit* with other compliant lan#!a#es. For example a '/ class can
inherit "rom a CQ class and oerride its irt!al methods.
C@%%@N %ET$@6S @F CTS:
Eer* t*pe s!pported b* CTS is deried "rom S*stem.<bAect. There"ore& eer*
t*pe s!pports the "ollowin# methods,
<. P)B&IC %ET$@6S:
o Boolean ECuals(@bDect#: %t is !sed to test eE!alit* eith another
obAect. 4e"erence t*pes sho!ld ret!rn tr!e i" the <bAect parameter
re"erences the same obAect and 'al!es t*pes when re"erences the
same al!e.
o Int >= 9et $as+ Code( #:
%t #enerates a n!mber correspondin# to the al!e o" an obAect. %" two
obAects o" the same t*pe are eE!al& then the* m!st ret!rn the same
hash code. This al!e is !sed extensiel* b* the sortin# al#orithms
implemented in S*stem.Collections.
o T4pe 9etT4pe( #:
;ets a T*pe <bAect that can be !sed to access metadata associated
with the t*pe and as a startin# point "or nai#atin# the obAect
heirarch* exposed b* the 4e"lection AP%.
o Strin( ToStrin(( #:
The de"a!lt implementation ret!rns the "!ll* E!ali"ied name o" the
class o" the obAect. This method is o"ten oerridden to o!tp!t data that
is more meanin#"!l to the t*pe.
=. P0@TECTE6 %ET$@6S:
o 'oid 2inali8e( #:
This method is called b* the r!ntime to allow "or clean!p prior to
#arba#e collection.
o @bDect %ember*iseClone:
This member represents a shallow cop* o" the obAect containin#
re"erences to other obAects that does not incl!de copies o" the obAects
re"erenced.
SET @F &AN9)A9ES T$AT TA09ET T$E C&0:
Microso"t proided lan#!a#es that tar#et the CL4 incl!de 'is!al /asic& 'is!al
CBB with Mana#ed Extensions& 'is!al CQ& and 3Script. Third parties are also
proidin# man* other lan#!a#esMMMMtoo man* to list here.
CTS BENEFITS:
The bene"its o" the common t*pe s*stem incl!de,
o &an(ua(e interoperabilit4:
Since all .Net lan#!a#es are !sin# a sin#le t*pe s*stem& *o! are
ass!red that obAects and t*pes are created in di""erent lan#!a#es can
interact with one another in a seamless manner. %tDs the CTS0CLS
combination that helps ma$e lan#!a#e interoperabilit* more than A!st
a pro#rammerDs dream.
o Sin(l4 rooted obDect +ierarc+4:
A sin#l* rooted obAect hierarch* is the $e* to a !ni"ied t*pe s*stem
beca!se it #!arantees that each obAect in the hierarch* has a common
inter"ace and there"ore eer*thin# in the hierarch* will !ltimatel* be o"
the same base t*pe.
o T4pe sa2et4:
T*pe sa"et* #!arantees that t*pes are declared sa"el* and there is no
wa* to tric$ the s*stem to thin$ that one t*pe is act!all* another and
onl* appropriate "!nctions can be pre"ormed on a partic!lar obAect.
W+at is .Net 0emotin(B
.Net remotin# replaces :C<M. Web Serices that !ses remotin# can r!n in an*
Application t*pe i.e. Console Application& Windows Form Applications& Window
Serices etc.
We will hae a wal$ thro!#h o" CL4 <bAect 4emotin#. %n CL4 <bAect 4emotin# we can
call obAects across networ$& as i" called locall*.
6istributed Identities
%" we pass a re"erence to a remote obAect& we will access the same obAect !sin# this
re"erence.
&easeEBased &i2etime
.Net 4emotin# ta$es a LeaseMbase Li"etime o" the obAect that is scaleable and
di""erent "rom pin# mechanism !sed b* :C<M.
Call Conte,t
Additional in"ormation can be passed with eer* method call that is not part o" the
ar#!ment with the help o" S<AP Header.
.NET 0E%@TIN9 A0C$ITECT)0E
Methods that will be called "rom the client are implemented in a remote obAect class.
Client !ses a prox* to call a remote obAect.
4emote obAects r!ns inside a process that is di""erent "rom the client process Y
!s!all* a di""erent s*stem Y the client canDt call it directl*.
For the client& the prox* loo$s li$e the real obAect with the same p!blic methods.
When the methods o" the prox* are called& messa#es are created.
Messa#es are serialiLed !sin# a "ormatter class& and are sent into a client channel.
The client channel comm!nicates with the serer part o" the channel to trans"er the
messa#e across the networ$.
The serer channel !ses a "ormatter to deserialiLe the messa#e& so that the methods
can be dispatched to the remote obAect.
The "ormatter and the prox* is s!pplied a!tomaticall*.
0emote @bDects
A remote obAect is implemented in a class that deries "rom
S*stem.Marshal/*4e"<bAect. A client doesnDt call the methods directl*6 instead a
prox* obAect is !sed to ino$e methods on the remote obAect. Eer* p!blic method
that we de"ine in the remote obAect class is aailable to be called "rom clients.
%[email protected]
!sin# S*stem6
namespace Hemsheel.Samples
Z
p!blic class M*4emote<bAect,S*stem.Marshal/*4e"<bAect
Z
p!blic M*4emote<bAect()
Z
Console.WriteLine(.M*4emote<bAect Constr!ctor
Called.)6
[
p!blic strin# Hello()
Z
Console.WriteLine(.Hello Called.)6
ret!rn .Hello& .Net ClientI.6
[
[
[
/!ild the assembl* M*4emote<bAect "rom the class M*4emote<bAect csc
5t:librar4 5out:%[email protected] %[email protected]
Ser'er
The remote obAect needs a serer process where it will be instantiated. This serer
has to create a channel and p!t it into listenin# mode so that clients can connect to
this channel.
Ser'er Con2i(uration File
%n Con"i#!ration "ile channel& endpoint& name etc. can be chan#ed witho!t a
recompile. When the client connects to the remote obAect it needs to $now the 54% o"
the obAect& i.e. the name o" the host where the remote obAect is r!nnin#& the protocol
and port n!mber to connect to& the name o" the serer& and the name o" the remote
obAect. With the exception o" the host name we hae to speci"* all these items in the
con"i#!ration "ile.
tcp:55local+ost:FGGG5SimpleSer'er5%40emote@bDect
In SimpleSer'er.e,e.con2i(
All o" the remotin# con"i#!rations m!st be added as child elements
to +s*stem.r!ntime.remotin#1.
The +application1 element speci"ies the name o" the serer with the name attrib!te.
The application o""ers a serice and reE!ires the con"i#!ration o" channels "or the
serice.
We hae the +serice1 and +channel1elements.
The serice that is o""ered "rom the application m!st be listed as a child o"
+serice1. This is the remote obAect itsel". The remote obAect is speci"ied with the
+well$nown1 element.
For instantiatin# the obAect the "ramewor$ reE!ires the name o" the assembl* to
$now where the t*pe o" this obAect can be loaded "rom. We can set this in"ormation
with the HML attrib!te t*pe.t*pe-.Hemsheel.Samples. M*4emote<bAect&
M*4emote<bAect. de"ines that the t*pe o" the remote obAect is M*4emote<bAect in
the namespace Hemsheel.Samples& and it can be "o!nd in the assembl*
M*4emote<bAect. The mode attrib!te is set to Sin#leCall.
We re"erence the prede"ined serer channel !sin# the TCP protocol, tcp serer. We
assi#n the port o" this channel with the port attrib!te& as the serer m!st hae a
wellM$nown port n!mber that the client m!st be aware o".
SimpleSer'er.e,e.con2i(
+con"i#!ration1
+s*stem.r!ntime.remotin#1
+application name - .SimpleSerer.1
+serice1
+well$nown
mode-.Sin#leCall.
t*pe-.Hemsheel.Samples.M*4emote<bAect&M*4emote<bAect.
obAect5ri-.M*4emote<bAect.01
+0serice1
+channels1
+channel re"-.tcp serer. port-.W888.01
+0channels1
+0application1
+0s*stem.r!ntime.remotin#1
+0con"i#!ration1
The Serer is implemented in the console application.
4emotin#Con"i#!ration.Con"i#!re() reads the con"i#!ration "ile to con"i#!re
SimpleSerer.exe.con"i# to con"i#!re and actiate the channels. We hae to $eep the
process alie.
SimpleSer'er.cs
!sin# S*stem6
!sin# S*stem.4!ntime.4emotin#6
namespace Hemsheel.Samples
Z
class SimpleSerer
Z
static oid Main(strin# \] ar#s)
Z
4emotin#Con"i#!ration.Con"i#!re(.SimpleSerer.exe.con"i#.)6
Console.WriteLine(.Press ret!rn to exit.)6
Console.4eadLine()6
[
[
[
Compile "rom dos prompt
csc 0t,exe SimpleSerer.cs
Client
Create a client !sin# a con"i#!ration "ile
Client Con"i#!ration File
The client con"i#!ration "ile SimpleClient.exe.con"i# !ses the HML +client1 element
to speci"* the 54L o" the serer !sin# protocol,00hostname,port0application. We !se
tcp as the protocol& and the serer r!ns on localhost with the port n!mber W888. The
application name o" the serer is de"ined with the name attrib!te o" the
+application1 element in the serer con"i#!ration "ile. The +well$nown1 element
speci"ies the remote obAect we want to access. The t*pe attrib!te de"ines the t*pe o"
the remote obAect and the assembl*. The !rl attrib!te de"ines the path to the remote
obAect. Appended to the 54L o" the application is the endpoint name
M*4emote<bAect. The channel that is con"i#!red with the client can a#ain be "o!nd in
the con"i#!ration "ile machine.con"i#.
SimpleClient.e,e.con2i(
+con"i#!ration1
+s*stem.r!ntime.remotin#1
+application name - .SimpleClient.1
+client !rl-.tcp,00localhost,W8880SimpleSerer.1
+well$nown
t*pe-.Hemsheel.Samples.M*4emote<bAect&M*4emote<bAect.
!rl-.tcp,00localhost,W8880SimpleSerer0M*4emote<bAect.01
+0client1
+channels1
+channel re"-.tcp client.01
+0channels1
+0application1
+0s*stem.r!ntime.remotin#1
+0con"i#!ration1
Client Application Actiate the client channel b* callin#
4emotin#Con"i#!ration.Con"i#!re(). 5sin# con"i#!ration "iles we can simpl* !se new
to create the remote obAect. Next we call the method Hello() o" this obAect.
SimpleClient.cs
!sin# S*stem6
!sin# S*stem.4!ntime.4emotin#6
namespace Hemsheel.Samples
Z
class SimpleClient
Z
static oid Main(strin#\] ar#s)
Z
4emotin#Con"i#!ration.Con"i#!re(.SimpleClient.exe.con"i#.)6
M*4emote<bAect robA - new M*4emote<bAect()6
Console.WriteLine(robA.Hello())6
[
[
[
Compile it
csc 0tar#et,exe 0re"erence,M*4emote<bAect.dll SimpleClient.cs
Note,Please r!n the Serer "irst.Ceep it r!nnin#.And then r!n the client.
Happ* Codin#
%icroso2t .NET Securit4 E For Ne* Comers
Bac! 9round
.NET technolo#* sec!rit* crosses .process bo!ndaries. and een .machine
bo!ndaries. to preent access to sensitie data or reso!rces in a distrib!ted
application enironment. This statement is with re"erence to %nside CQ b* Tom
ArcherI
The "ollowin# are some o" the basic elements o" the .NET sec!rit* s*stem,
@. EidenceMbased sec!rit* is a new concept in .NET Framewor$.
An assembl* contains seeral important pieces o" in"ormation that ma* be
applied to decide what leel o" access to #rant the component. Some o" the
in"ormation !sed incl!des what site the component was downloaded "rom&
what Lone that site was in& (%nternet& intranet& local machine& and so on) and
the stron# name o" the assembl*. The stron# name implies to an encr*pted
identi"ier that !niE!el* de"ines the assembl* and con"irms that it has not been
tampered with.
=. The .NET Common Lan#!a#e 4!ntime proides sec!rit* !sin# Polic*M:rien
Tr!st Model !sin# Code Eidence.
%t so!nds worse than it reall* is. Essentiall* this is a s*stem o" sec!rit*
policies that can be set b* an administrator to allow certain leels o" access
based on the componentDs assembl* in"ormation. The policies are set at three
leels,
a. The Enterprise
b. The %ndiid!al Machine
c. and The 5ser.
7. Callin# .NET Framewor$ methods "rom the /ase Class Librar* #ettin# the
bene"its o" the b!ilt in sec!rit*.
That is& the deeloper doesnDt hae to ma$e explicit sec!rit* calls to access
s*stem reso!rces. Howeer& i" *o!r components expose inter"aces to
protected reso!rces& *o! will be expected to ta$e the appropriate sec!rit*
meas!res.
N. 4oleMbased sec!rit* pla*s a part in the .NET sec!rit* scheme.
Man* applications need to restrict access to certain "!nctions or reso!rces
based on the !ser& and .NET introd!ces the concepts o" identities and
principals to incorporate these "!nctions.
J. Now A!thentication and a!thoriLation "!nctions are accessed thro!#h a sin#le
AP%.
%t can easil* be extended to incorporate applicationMspeci"ic lo#ic as reE!ired.
A!thentication methods incl!de basic operatin# s*stem !ser identi"ication&
basic HTTP&ASP.NET "orms& :i#est and Cerberos& as well as the new .NET
serice& Microso"t .NET Passport.
T. 9esI %solated stora#e is a special area on dis$ assi#ned to a speci"ic assembl*
b* the sec!rit* s*stem.
No access to other "iles or data is allowed& and each assembl* !sin# isolated
stora#e is separated "rom each other. %solated stora#e ma* be applied "or a
sain# a components state& or sain# settin#s& and ma* be applied b*
components that do not hae access to read and write "iles on the s*stem.
U. A rob!st set o" cr*pto#raphic "!nctions that s!pport encr*ption& di#ital
si#nat!res& hashin#& and randomMn!mber #eneration are incl!ded in the .NET
Framewor$.
These are implemented !sin# al#orithms& s!ch as 4SA& :SA& Triple :ES& :ES&
and 4C=& as well as the M:J& SHA@ and SHAMJ@= hash al#orithms. Moreoer&
the HML :i#ital Si#nat!re speci"ication& !nder deelopment b* the %nternet
En#ineerin# Tas$ Force (%ETF) and the World Wide Web Consorti!m (W7C)& is
also aailable. The .NET Framewor$ !ses these cr*pto#raphic "!nctions to
s!pport ario!s internal serices.