个性化定制ASP NET Whidbey中英文对照外文翻译文献.docx
《个性化定制ASP NET Whidbey中英文对照外文翻译文献.docx》由会员分享,可在线阅读,更多相关《个性化定制ASP NET Whidbey中英文对照外文翻译文献.docx(14页珍藏版)》请在冰点文库上搜索。
个性化定制ASPNETWhidbey中英文对照外文翻译文献
中英文资料对照外文翻译
原文:
GetPersonalwithASP.NETWhidbey
ConfiguretheProvider
Withbothpersonalizationandmembership,thefirststepisconfiguringtheproviderthatyouwillusetostorethepersonalizationormembershipdata.ThoughyoucancreatetheMicrosoftAccessorMicrosoftSQLServer™databaseandaddthenecessaryconfigurationelementsmanually,theeasierwayistousetheASP.NETWebSiteAdministrationtool,Notethattoconfigureanapplicationsuccessfully,youmustbeloggedinusinganaccountwithadministratorrights(youalsocanlaunchMicrosoftVisualStudio®.NETwithanadministrator-levelaccountusingRunAs...andlaunchtheWebSiteAdministrationtoolfromthebuttoninSolutionExplorer).
TheASP.NETWebSiteAdministrationtoolprovidesthemeanstoconfigurepersonalizationandmembershipfeatures(theMembershipdatastoreisconfiguredusingtheSecuritytab),aswellasreportsanddata-accessfeatures.
TocreateanAccess.mdbfileforstoringpersonalizationdata,youneedtoopentheWebSiteAdministrationtool;thefile,namedAspNetDB.mdb,willbecreatedautomaticallyinafoldernamedDATA.AlthoughnotenabledinthebuildofVisualStudioagainstwhichthisarticlewaswritten,theWebSiteAdministrationtoolcontainsanentiresectiondevotedtoconfiguringpersonalizationsettings.Inalatersection,I'llwalkyouthroughaddingthenecessaryconfigurationsectionsbyhand.
YouconfiguretheprovidertouseformembershipservicesusingtheSecuritytaboftheWebSiteAdministrationtool.TheeasiestwaytoconfigurethemembershipprovideristoselecttheSecuritySetupWizard.I'llwalkyouthroughthisprocessmomentarily.
Atthispoint,themembershipdatabasewillbecreated,andthenecessaryconfigurationelementswillbeaddedtotheweb.configfile.Allyouneedtodofromhereisadduserstothedatabase(whichyoucandousingtheWebSiteAdministrationtool,orthemembershipAPIs),setauthorizationrestrictionsonpagesasdesired,andcreatealoginpage.
Itisimportanttonotethatthedatabasestructurethatiscreatedforbothpersonalizationandmembershipisthesame,soyoucan(andforefficiency'ssake,should)usethesameproviderforbothpersonalizationandmembership.Thatsaid,itispossibletouseadifferentproviderforpersonalizationthanformembership,andvice-versa,ifyouprefer.
Inadditiontothebuilt-inAccessandSQLServerproviders,youcancreateyourowncustomprovidersandconfigureyourapplicationstousetheseproviders.So,ifyoualreadyhaveauser-credentialdatabasethatyou'renotwillingtopartwith,ASP.NETallowsyoutousethatandstillgetthebenefitsthatmembershipservicesprovide.Notethatatthetimeofthiswriting,theactualmeansforcreatingcustomproviderscouldundergosomechangesstill,soI'llsaveademonstrationofcreatingcustomprovidersforafuturearticle.
How'stheDataStored?
UseServerExplorertoseehowdataisstoredinAspNetDB.mdb.JustcreateadatabaseconnectiontoAspNetDB.mdbanddragtablesfromtheconnectiontoapageinyoursite.VisualStudiowillcreateaGridViewcontrolandbindittoanAccessDataSourcecontrol(notethattheASP.NETworkerprocessmusthaveread-writepermissionsonthefoldercontainingthedatabaseforthistowork).Ifyouhavedifficultybrowsingpagesintheapplication,closetheconnectioninServerExplorerbeforebrowsingthepages.
PersonalizationandMembership:
Whatdotheymean?
Personalizationandmembershipenableyoutocontrolaccesstoyourapplication,aswellastostoreandretrieveinformationaboutusersofyourapplication,includinganonymoususers.Youcancustomizetheappearanceandbehaviorofyourapplicationbasedonthisinformation,andyouevencanallowuserstostoreprofileinformation,suchasashoppingcart,whilebrowsinganonymously,andlatereasilymigratethatinformationtotheirpersonalprofileswhentheylogin.
Personalizationallowsyoutostoreprofileinformationaboutusersofyourapplicationinapersistentdatastore.Personalizationsupportsapluggabledata-providerlayerandasetofAPIsforstoringandretrievingprofileinformationinastronglytypedfashion.Personalizationallowsyoutospecifyoneormorearbitrarypropertiestobestoredinauser'sprofile.Youcanspecifythetypeofeachproperty(whichcanbeasystemtypeorauser-definedtypeorcustomclass),aswellaswhetherthepropertyistrackedforanonymoususers,whetherthepropertyisread-onlyorread-write,andmore.
Personalizationalsocanbeintegratedwithmembershipservicestoprovideaunifiedsolutionforusermanagement,login,andprofile-informationstorage.Bydefault,theASP.NETpersonalizationsystemassociatesprofileinformationwiththeidentitywithwhichtheuserauthenticates,accessiblethroughHttpContext.Current.User.Identity.Name.IfyouareusingASP.NETmembershipservicesforuser-credentialmanagement,thenanytimeauserlogsintoyourapplication,hisorhermembershipidentityautomaticallywillbestoredinHttpContext.Current.User.Identity.Name,andallprofileinformationassociatedwiththatidentitywillbeavailabletotheapplication.SupportforstoringprofileinformationforanonymoususersisnotenabledbydefaultandrequiresaddinganelementtotheWeb.configfilefortheapplication,aswellasspecificallymakingeachdesiredpropertyavailableforanonymoususers.
Membershipdescribesthesetoftechnologies,including(aswithpersonalization)aback-endproviderforstoringdata;asetofAPIsformanagingusersandlogins,andsoon;andcontrolsthatallowyoutoadduser-credentialstorageandrelatedfunctionalitytoyourapplicationwithnolinesofcode.
Usercredentialsarestoredinaback-endmembershipdatabasespecifiedbythedataprovideryouconfigureinWeb.config.ASP.NETWhidbeyshipswithAccess,andSQLServerprovidersareavailableoutofthebox.Oncemembershipisconfigured,andusersareaddedtothemembershipdatastore,addingloginfunctionalitytotheapplicationcanbeassimpleasdraggingasinglecontroltoapageintheapplication.TheASP.NETlogincontrols(Login,LoginView,LoginStatus,LoginName,andPasswordRecovery)containallofthelogicnecessarytovalidatecredentialsandperformanynecessaryredirection,andsoon,andaredesignedtointegratewithmembership.
AddPersonalizationProperties
Todemonstratepersonalization,nextI'llshowyouhowtoaddsomepropertydefinitionsandstoreandretrievethemfromapage.Oneofthepropertieswillallowtheusertochooseapagethemethatwillbeusedwhenevertheuservisits.ThemesareanewfeatureofASP.NETWhidbeythatallowyoutomodifythelookandfeelofanentiresitewithasimpleconfigurationsettingorafewlinesofcode.
OpenWeb.configandaddthefollowing,directlyaftertheelement:
type=
"System.Collections.Specialized.StringCollection"
allowAnonymous="true"
serializeAs="Xml"/>
Theelementisrequiredinordertoallowanonymousaccesstoanypersonalizationproperties.Thepersonalizationsectioncontainstwoproperties,bothofwhichusetheallowAnonymousattributetoenablethepropertiestobetrackedforuserswhoarenotloggedin.Thefirstproperty,Theme,doesnotspecifyatype,soitwillbetreatedasastring.Thesecondproperty,FavoriteColors,specifiestheStringCollectionclassasitstype.AnyattempttostoredatathatisnotcompatiblewiththeStringCollectionclassinthispropertywillresultinanexceptionbeingthrown.TheserializeAsattributeallowstheStringCollectiontobestoredinthedatabaseasanXMLstring.
CreateanewWebFormintheprojectcalledDefault.aspx.Then,switchtoDesignviewandaddthecontrols,withtheirpropertiessetasspecified.
Table1.Propertiestobeassignedtothecontrolsaddedintheprecedingexamplestep
Control
Properties
DropDownList
ID=Themes
Button
ID=SetTheme
Text=SetTheme
TextBox
ID=textFavColor
Button
ID=AddColor
Text=AddColor
ListBox
ID=listFavColors
SelecttheDropDownListcontrolandinthePropertieswindow,scrolldowntoandselecttheItemsproperty.ClicktheellipsisbuttontoopentheCollectionEditor.Addtwoitems,onewiththetextandvaluesettoBasicBlueandonesettoSmokeAndGlass,andthenclickOK.Double-clicktheSetThemebuttonandaddthefollowingcodetotheeventhandler:
Profile.Theme=Themes.SelectedValue
AddthefollowingeventhandlertotheServerCodewindow:
SubPage_PreInit(ByValsenderAsObject,_
ByValeAsSystem.EventArgs)
IfProfile.Theme=""Then
IfRequest.Form("Themes")<>""Then
Page.Theme=Request.Form("Themes")
EndIf
Else
Page.Theme=Profile.Theme
EndIf
EndSub
Thiscodeisrequiredtosetthepage'stheme,whichmustbesetinthePage_PreIniteventorearlier.Thecodecheckstoseewhetherathemeisalreadysetfortheuser'spersonalizationprofileandusesthattheme.Ifnothemeexists,thecodecheckstoseeiftheuserhassubmittedthepagewithanewthemechoiceand,ifso,usesthenewtheme.Otherwise,nothemewillbeapplied.
SwitchbacktoDesignviewanddouble-clicktheAddColorbutton.Addthefollowingcodetotheeventhandler:
DimFaveColorAsString=_
Server.HtmlEncode(textFavColor.Text)
DimFaveColorsAsNew_
System.Collections.Specialized.StringCollection
Profile.FavoriteColors.Add(FaveColor)
DisplayFavoriteColors()
AddthefollowingsubroutinejustbelowtheAddColor_Clickhandler:
SubDis