个性化定制ASP.NET Whidbey中英文对照外文翻译文献_第1页
个性化定制ASP.NET Whidbey中英文对照外文翻译文献_第2页
个性化定制ASP.NET Whidbey中英文对照外文翻译文献_第3页
个性化定制ASP.NET Whidbey中英文对照外文翻译文献_第4页
个性化定制ASP.NET Whidbey中英文对照外文翻译文献_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PAGE1中英文资料对照外文翻译原文:GetPersonalwithASP.NETWhidbeyConfiguretheProviderWithbothpersonalizationandmembership,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.AddPersonalizationPropertiesTodemonstratepersonalization,nextI'llshowyouhowtoaddsomepropertydefinitionsandstoreandretrievethemfromapage.Oneofthepropertieswillallowtheusertochooseapagethemethatwillbeusedwhenevertheuservisits.ThemesareanewfeatureofASP.NETWhidbeythatallowyoutomodifythelookandfeelofanentiresitewithasimpleconfigurationsettingorafewlinesofcode.OpenWeb.configandaddthefollowing,directlyafterthe<system.web>element:<anonymousIdentificationenabled="true"/><personalization><profile><propertyname="Theme"allowAnonymous="true"/><propertyname="FavoriteColors"type="System.Collections.Specialized.StringCollection"allowAnonymous="true"serializeAs="Xml"/></profile></personalization>The<anonymousIdentification>elementisrequiredinordertoallowanonymousaccesstoanypersonalizationproperties.Thepersonalizationsectioncontainstwoproperties,bothofwhichusetheallowAnonymousattributetoenablethepropertiestobetrackedforuserswhoarenotloggedin.Thefirstproperty,Theme,doesnotspecifyatype,soitwillbetreatedasastring.Thesecondproperty,FavoriteColors,specifiestheStringCollectionclassasitstype.AnyattempttostoredatathatisnotcompatiblewiththeStringCollectionclassinthispropertywillresultinanexceptionbeingthrown.TheserializeAsattributeallowstheStringCollectiontobestoredinthedatabaseasanXMLstring.CreateanewWebFormintheprojectcalledDefault.aspx.Then,switchtoDesignviewandaddthecontrols,withtheirpropertiessetasspecified.Table1.PropertiestobeassignedtothecontrolsaddedintheprecedingexamplestepControlPropertiesDropDownListID=ThemesButtonID=SetThemeText=SetThemeTextBoxID=textFavColorButtonID=AddColorText=AddColorListBoxID=listFavColorsSelecttheDropDownListcontrolandinthePropertieswindow,scrolldowntoandselecttheItemsproperty.ClicktheellipsisbuttontoopentheCollectionEditor.Addtwoitems,onewiththetextandvaluesettoBasicBlueandonesettoSmokeAndGlass,andthenclickOK.Double-clicktheSetThemebuttonandaddthefollowingcodetotheeventhandler:Profile.Theme=Themes.SelectedValueAddthefollowingeventhandlertotheServerCodewindow:SubPage_PreInit(ByValsenderAsObject,_ByValeAsSystem.EventArgs)IfProfile.Theme=""ThenIfRequest.Form("Themes")<>""ThenPage.Theme=Request.Form("Themes")EndIfElsePage.Theme=Profile.ThemeEndIfEndSubThiscodeisrequiredtosetthepage'stheme,whichmustbesetinthePage_PreIniteventorearlier.Thecodecheckstoseewhetherathemeisalreadysetfortheuser'spersonalizationprofileandusesthattheme.Ifnothemeexists,thecodecheckstoseeiftheuserhassubmittedthepagewithanewthemechoiceand,ifso,usesthenewtheme.Otherwise,nothemewillbeapplied.SwitchbacktoDesignviewanddouble-clicktheAddColorbutton.Addthefollowingcodetotheeventhandler:DimFaveColorAsString=_Server.HtmlEncode(textFavColor.Text)DimFaveColorsAsNew_System.Collections.Specialized.StringCollectionProfile.FavoriteColors.Add(FaveColor)DisplayFavoriteColors()AddthefollowingsubroutinejustbelowtheAddColor_Clickhandler:SubDisplayFavoriteColors()listFavColors.DataSource=Profile.FavoriteColorslistFavColors.DataBind()EndSubAddthefollowinglinetothePage_Loadeventhandler(ifnecessary,switchtoDesignviewanddouble-clickanemptyareaofthepagetoaddthePage_Loadhandler):DisplayFavoriteColors()Now,savethepage.TestthePersonalizationSettingsBrowsethepage,selectathemefromtheDropDownListcontrolandclickSetTheme.Youshouldseethethemeappliedtothecontrols.Next,typethenameofacolorinthetextboxandclickAddColor.Thecolorwillbeaddedtothelistbox,whichispopulatedfromtheprofile.Afterapplyingathemeandaddingacoupleofcolors.Uptothispoint,thepersonalizationinformationisbeingstoredexclusivelyforanonymoususers.Butwhatifyouwanttotaketheinformationthat'salreadybeensavedforananonymoususerandmigrateittoaspecificprofileforauserwhenheorshelogsin?Here'show:AddaGlobal.asaxfiletotheWebsitebyright-clickingthesiteinSolutionExplorer,selectingAddNewItem,andchoosingtheGlobalApplicationClasstemplate.Then,addthefollowingcodetoGlobal.asax:SubPersonalization_MigrateAnonymous(senderAsObject,_eAsPersonalizationMigrateEventArgs)Profile.Theme=_Profile.GetProfile(e.AnonymousId).ThemeProfile.FavoriteColors=_Profile.GetProfile(e.AnonymousId).FavoriteColorsEndSubInDesignview,addaLogincontrolandaLoginNamecontrol(foundontheSecuritytabofthetoolbox)toDefault.aspx,belowtheothercontrols,thensaveandbrowsethepage.Whenthepageisfirstdisplayed,nousernamewillbedisplayedbytheLoginNamecontrol,andthepagewilldisplayanypropertiesyoupreviouslyhadsetwhilebrowsinganonymously.Loginusingtheaccountcredentialsyouaddedwhenconfiguringthemembershipdatabase.TheLoginNamecontrolwilldisplayyouruserIDnow,andtheThemeandFavoriteColorspropertieshavebeenmigratedtotheprofileforyourlogged-inaccount.Notethatifyouloginandthenlogoutagain,anewanonymousidentityiscreated,andanypersonalizationforthepreviousanonymousidentityisnolongerdisplayed.SummaryInthisarticle,I'vedemonstratedhowthenewpersonalizationandmembershipfeaturesofASP.NETWhidbeyprovidepowerfulfunctionalitytoyourWebapplicationswhilerequiringverylittleeffort(andevenlesscode!)toconfigureanduse.Inadditiontothescenariosdemonstratedinthisarticle,personalizationservicescanbeusedinconjunctionwiththenewWeb-partsfeatureofASP.NETWhidbeytocreatepowerfulandeasilycustomizableportals.Usingpersonalizationandmembership,itisnowpossibletocreaterich,customizedWebapplicationswithrobustsecuritywhilewritinglittleornoplumbingcode,leavingyoumoretimetofocusonthebusinesslogicthatenablesthefeaturesyourusersactuallycareabout.中文:个性化定制ASP.NETWhidbey配置提供程序要使用个性化定制和成员身份,第一步是配置将用于存储个性化定制或成员身份数据的提供程序。虽然您可以创建MicrosoftAccess或MicrosoftSQLServer?数据库并手动添加必要的配置元素,但更简单的方法是使用ASP.NETWeb站点管理工具,请注意,要成功配置应用程序,您必须使用具有管理员权限的帐号登录(您也可以通过RunAs…使用管理员级别的帐号启动MicrosoftVisualStudio.NET,并从SolutionExplorer中的按钮启动Web站点管理工具。)。ASP.NETWeb站点管理工具提供了一些方法,用于配置个性化定制和成员身份功能(成员身份数据的存储使用Security选项卡来配置)以及报表和数据访问功能。要创建用于存储个性化数据的Access.mdb文件,您需要开启Web站点管理工具;名为AspNetDB.mdb的文件将在名为DATA的文件夹中自动创建。尽管在本文针对的VisualStudio版本中未启用,但Web站点管理工具包含一个完整的部分,专门用于配置个性化设置。在后面的小节中,我将一步步引导您手动添加必要的配置部分。您可以使用Web站点管理工具的Security选项卡来配置提供程序,以用于成员身份服务。配置成员身份提供程序的最简单方法是选择SecuritySetupWizard。稍后我将引导您完成此过程。Web站点管理工具的Security选项卡提供了一个向导,便于设置成员身份数据存储区,还提供了一组管理工具,用于在创建数据存储区之后进行修改。现在,将创建成员身份数据库,并向web.config文件中添加必要的配置元素。此处您需要做的只是向数据库添加用户(您可以使用Web站点管理工具或成员身份API来完成)、根据需要在页面上设置授权限制以及创建登录页面。请注意,为个性化定制和成员身份创建的数据库结构是相同的,因此可以(考虑到效率,您应该)对个性化定制和成员身份使用相同的提供程序,这一点很重要。也就是说,如果愿意,您可以对个性化定制使用与成员身份不同的提供程序,反之亦然。除了内置Access和SQLServer提供程序,您还可以创建自己的自定义提供程序,并配置您的应用程序以使用这些提供程序。因此,如果您已经拥有用户凭据数据库且不想放弃,ASP.NET可允许您使用该数据库并同时享受成员身份服务提供的好处。请注意,在撰写本文档时,创建自定义提供程序的实际方法可能还会经历一些更改,因此我将为将来的文章保存一个创建自定义提供程序的演示。数据是如何存储的?使用ServerExplorer查看数据如何存储在AspNetDB.mdb中。只需创建一个到AspNetDB.mdb的数据库连接,并把表从连接拖动到您站点的页面中。VisualStudio将创建一个GridView控件,并将它绑定到AccessDataSource控件(请注意,要使它工作,ASP.NET辅助进程必须对包含数据库的文件夹拥有读写权限)。如果您在浏览应用程序中的页面时有困难,请在浏览页面之前于ServerExplorer中关闭连接。个性化定制和成员身份:它们表示什么?个性化定制和成员身份使您能够控制对您应用程序的访问,以及存储和检索有关您应用程序用户(包括匿名用户)的信息。您可以根据这些信息自定义应用程序的外观和行为,甚至可以允许用户在匿名浏览时存储配置文件信息(例如购物车),并在他们以后登录时轻松地将那些信息迁移到他们的个人配置文件中个性化定制允许您将有关您应用程序用户的配置文件信息存储在持久性数据存储区中。个性化定制支持可接插式数据提供程序层,以及一组以强类型风格存储和检索配置文件信息的API。个性化定制可让您指定一个或多个要存储在用户配置文件中的任意属性。您可以指定每个属性的类型(它可以是系统类型、用户定义的类型或自定义类),以及是否跟踪匿名用户的属性、该属性是只读还是可读写,等等。个性化定制还可以与成员身份服务集成,以便为用户管理、登录和配置文件信息存储区提供统一的解决方案。默认情况下,ASP.NET个性化定制系统将配置文件信息与用户用来进行身份验证的标识相关联,该标识可通过HttpContext.Current.User.Identity.Name访问。如果您使用ASP.NET成员身份服务进行用户凭据管理,则无论何时用户登录您的应用程序,他/她的成员身份标识将自动存储在HttpContext.Current.User.Identity.Name中,并且应用程序可以使用与该标识关联的所有配置文件信息。默认情况下,不支持存储匿名用户的配置文件信息,且需要向应用程序的Web.config文件添加元素,还需要明确地让每个所需的属性对匿名用户可用。成员身份描述了一套技术,包括(与个性化定制一样)用于存储数据的后端提供程序、一组用于管理用户和登录的API以及一些控件(这些控件使您能够在不使用代码行的情况下就为应用程序添加用户凭据存储区及相关功能)等等。用户凭据存储在一个后端成员身份数据库中,该数据库由您在Web.config中配置的数据提供程序指定。ASP.NETWhidbey配套带有Access,并且SQLServer提供程序可以单独使用。一旦配置了成员身份并向成员身份数据存储区中添加了用户,只需将单个控件拖到应用程序的页面上,即可为应用程序添加登录功能。ASP.NET登录控件(Login、LoginView、LoginStatus、LoginName和PasswordRecovery)包含验证凭据和执行任何需要的重定向等所需的所有逻辑,并设计为与成员身份集成。添加个性化定制属性为了说明个性化定制,接着我将为您演示如何添加一些属性定义,以及如何从页面存储和检索它们。其中一个属性将允许用户选择页面主题,用户在任何时候访问该页面时都将使用此主题。主题是ASP.NETWhidbey的一个新功能,它可让您只用一个简单的配置设置或几行代码就能修改整个站点的外观和感觉。打开Web.config并将以下代码直接添加到<system.web>元素的后面:<anonymousIdentificationenabled="true"/><personalization><profile><propertyname="Theme"allowAnonymous="true"/><propertyname="FavoriteColors"type="System.Collections.Specialized.StringCollection"allowAnonymous="true"serializeAs="Xml"/></profile></personalization><anonymousIdentification>元素是允许匿名访问任何个性化定制属性所必需的。个性化定制部分包含两个属性,这两个属性都使用allowAnonymous属性来允许跟踪未登录用户的属性。第一个属性Theme未指定类型,所以被视为字符串。第二个属性FavoriteColors指定它的类型为StringCollection类。若尝试将与StringCollection类不兼容的数据存储在这个属性中,将导致引发异常。serializeAs属性可让StringCollection作为XML字符串存储在数据库中。在项目中创建一个名为Default.aspx的新Web窗体。然后,切换到设计视图并添加控件,并按指定方式设置它们的属性。表1.要分配给先前示例步骤中添加的控件的属性控件属性DropDownListID=ThemesButtonID=SetThemeText=SetThemeTextBoxID=textFavColorButtonID=AddColorButtonText=AddColorText=AddColorListBoxID=listFavColors选择DropDownList控件,并在属性窗口中向下滚动以选择Items属性。单击省略号按钮,打开集合编辑器。添加两项,其中一项的文本和值设置为BasicBlue,另一项设置为SmokeAndGlass,然后单击OK。双击SetTheme按钮,并将以下代码添加到事件处理程序中:Profile.Theme=Themes.SelectedValue将以下事件处理程序添加到服务器代码窗口中:SubPage_PreInit(ByValsenderAsObject,_ByValeAsSystem.EventArgs)IfProfile.Theme=""ThenIfRequest.Form("Themes")<>""ThenPage.Theme=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论