`
lovnet
  • 浏览: 6713165 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

如何在运行时改变User Profile的Provider

阅读更多

在asp.net 2.0中有专门的Profile Provider用于为User Profile提供下层的存储服务。(如果你对User Profile不熟悉,请看http://www.cnblogs.com/tonyqus/archive/2005/12/18/userprofilev2.html

当然,asp.net也允许我们定制自定义的User Profile Provider,这为多服务器或多种存储介质提供了方便。比如我们可以把Profile存储在Oracle的服务器上,通过实现System.Web.Profile.ProfileProvider抽象类自定义一个OracleProfileProvider就可以满足这样的需求。但通常我们都是在web.config中设置defaultProvider来切换Provider的,这意味着我们无法在运行时修改Provider。如果要用SqlProfileProvider就必须一直使用它。但这无法满足一些特殊的项目需求,比如根据用户的不同,使用不同的Profile数据库。

于是我在网上找了很多资料,但都没有有关如何在运行时修改Profile Provider的方法,只看到如何在运行时修改Membership Provider以及如何自定义Profile Provider的文章。为了实现这一点,特地用Reflector分析了默认的存储方法。得出的结论如下:

假设我们在web.config中有这样的profile设置:

<profiledefaultProvider="AspNetSqlProfileProvider">
<providers>
<clear/>
<addname="AspNetSqlProfileProvider"connectionStringName="strConnUserDB"type="System.Web.Profile.SqlProfileProvider"description="存储Profile数据"/>
<addname="TextFileProfileProvider"type="CustomProviders.TextFileProfileProvider,CustomProviders"description="Textfileprofileprovider"/>
</providers>
<properties>
<addname="test"/>
</properties>
</profile>

如果我们要让Profile["test"]使用TextFileProfileProvider来保存数据的话,我们可以用下面的语句:

ProfileCommon.Properties["test"].Provider=Profile.Providers["TextFileProfileProvider"];
Profile[
"test"]="bbb";

下面我们来分析一下原理。

我们通常使用的Profile其实是一个ProfileCommon的一个实例,Profile.Providers属性提供了在web.config的providers节中设置的自定义Provider,我们可以用Profile.Providers["ProviderName"]这样的代码来获得一个自定义Provider,注意这里的ProviderName是providers节中的每一个provider的name属性。由于Properties属性是静态属性,无法通过ProfileCommon的实例来调用,所以只能用类调用。

在ProfileCommon类内部有一个SettingsPropertyCollection用于存储所有的Profile属性,而其中每个SettingsProperty就代表一个Profile属性,对于每个SettingsProperty有三个必要元素:name, value和provider,这也是为什么我们可以对某个Profile属性设置provider。

分享到:
评论

相关推荐

    PortSight Secure Access v4.3.3012 Enterprise Edition

    the Role provider authorizes users to perform actions based on roles they have been assigned and the Profile provider allows the saving of additional user details. API Exposed Through WCF/Web ...

    terraform-provider-jenkins:用于管理jenkins作业配置的Terraform Provider

    然后,当您运行terraform init时,Terraform将从本地文件系统中提取二进制文件。 curl -s https://raw.githubusercontent.com/jgramoll/terraform-provider-jenkins/master/install.sh | bash 用法 证书 转到$ ...

    sociallogin 修改微信登录.zip

    nextcloud 私有云修改sociallogin 插件,支持微信登录。 涉及文件: ...sociallogin/3rdparty/hybridauth/hybridauth/...使用时在后台建立一个新的 Custom OAuth2,其中Internal name 填写 WeChat,其他参数参考微信要求。

    terraform-provider-centrifyvault:Centrify Vault的Terraform提供商

    默认情况下,该目录的名称位于您的主目录内,因此在Unix上为$ HOME / go,在Windows上为%USERPROFILE%\ go(通常为C:\ Users \ YourName \ go)。命令go env GOPATH打印当前有效的GOPATH。如果未设置环境变量,它...

    Laravel 5.x Cookbook (Packt 2016)

    Adding a file upload to user profile 99 Validating the file upload 108 Saving files to S3 112 Chapter 6: Adding Angular to Your App 123 Introduction 123 Adding Angular search to our search page 130 ...

    atom-build-makensis-wine:用于在Wine上进行makensis的Atom Build提供程序,编译NSIS

    $ cd $ Env: USERPROFILE \.atom\packages :: Command Prompt $ cd %USERPROFILE% \.atom\packages Linux和macOS $ cd ~ /.atom/packages/ 以build-makensis-wine克隆存储库: $ git clone https://githu

    ReactiveMoya

    React性莫亚 使用Moya + ReactiveCocoa的更简单方法。 安装 #### Carthage将此行添加... UserProfile &#40; " ashfurrow " &#41;) . subscribeNext ({ (object) -&gt; Void in if let response = object as? MoyaRespon

    windowsnt 技术内幕

    创建宿主文件夹 设置用户登录时间 创建临时用户帐号 重新设置用户帐号口令 修改多个用户帐号 自动注销有时间限制的用户 要求用户在下次登录时改变口令 设置帐号规则 设置用户口令永不过期 停用用户帐号 解开登录失败...

    Silverlight 4: Problem - Design - Solution

    * Includes step-by-step deployment instructions to host the site with a real shared hosted provider, Discount ASP.NET Wrox Problem – Design – Solution references give you solid, workable solutions ...

    Silverlight 4 Problem - Design - Solution.pdf

    Includes step-by-step deployment instructions to host the site with a real shared hosted provider, Discount ASP.NET Wrox Problem – Design – Solution references give you solid, workable solutions to...

    full-stack-coding-challenge

    纽约市议会全栈编码挑战赛 从您收到我们办公室的挑战开始,您将有整整1周的时间来完成此挑战。 您可以通过将挑战推送到自己... 一个SQLite3数据库,该数据库已经带有用户数据,UserProfile数据和投诉数据。 每次登录均

    2.ASP.NET.2.0.高级编程(第4版) [1/7]

    书中提供了大量的实例,可帮助读者快速掌握如何在.NET平台下开发功能强大的ASP.NET应用程序。本书适合有一些基础的ASP.NET初级程序员以及准备迁移到ASP.NET 2.0的编程老手。该书与《ASP.NET 2.0入门经典(第4版)》...

    asp.net知识库

    在.NET访问MySql数据库时的几点经验! 自动代码生成器 关于能自定义格式的、支持多语言的、支持多数据库的代码生成器的想法 发布Oracle存储过程包c#代码生成工具(CodeRobot) New Folder XCodeFactory3.0完全攻略--...

    ASP.NET2.0高级编程(第4版)1/6

     运行时设置997 27.2.13 配置ASP.NET  工作进程1001 27.2.14 存储与应用程序  相关的设置1003 27.2.15 配置文件的编程1004 27.2.16 保护配置设置1011 27.2.17 编辑配置文件1011 27.3 小结1012 第28章 管理1013 ...

    PowerShellPack

    About_provider.help.txt About_quoting_rules.help.txt About_redirection.help.txt About_ref.help.txt About_regular_expression.help.txt About_reserved_words.help.txt About_scope.help.txt About_script_...

    Artech House - SMS and MMS Interworking in Mobile Networks

    Operator Is Not Its Own SCCP Provider: Use a Conversion Unit 135 7.4.4 Transmission of Signaling Between a GSM and an IS-41 Network 136 7.5 Conclusion 136 Reference 136 CHAPTER 8 Connecting ASPs and ...

    EurekaLog_7.5.0.0_Enterprise

    5)....Added: Exception2HRESULT in EAppDLL to simplify developing DLLs with "DLL" profile 6)....Added: Use ShellExecute option for mailto send method 7)....Added: "Mandatory e-mail only when sending" ...

    hm1375_parrotv1.1验证通过_20170824_1528.7z

    -- Each camcorder profile defines a set of predefined configuration parameters --&gt; &lt;!-- Back Camera --&gt; &lt;!-- Front Camera --&gt; bitRate="9000000" width="1920" height="1080" frameRate="24...

    opensc-0.12.0.tar.gz

    NEWS for OpenSC -- History of user visible changes Complete change history is available online: http://www.opensc-project.org/opensc/timeline New in 0.12.0; 2010-12-22 * OpenSC uses a single reader ...

Global site tag (gtag.js) - Google Analytics