惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
SecWiki News
SecWiki News
T
The Exploit Database - CXSecurity.com
S
Security @ Cisco Blogs
H
Heimdal Security Blog
Security Latest
Security Latest
T
Threatpost
V2EX - 技术
V2EX - 技术
C
Cybersecurity and Infrastructure Security Agency CISA
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
Recent Announcements
Recent Announcements
P
Privacy International News Feed
K
Kaspersky official blog
P
Proofpoint News Feed
L
LangChain Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
博客园_首页
T
Tor Project blog
M
MIT News - Artificial intelligence
The Hacker News
The Hacker News
The GitHub Blog
The GitHub Blog
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Full Disclosure
MyScale Blog
MyScale Blog
The Register - Security
The Register - Security
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
T
The Blog of Author Tim Ferriss
S
Schneier on Security
小众软件
小众软件
The Cloudflare Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Know Your Adversary
Know Your Adversary
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
Vercel News
Vercel News

博客园 - wdx2008

ORM框架 sql语句字符串处理大全 - wdx2008 - 博客园 收集的ASP.NET中常用正则表达式 - wdx2008 - 博客园 web.config详解 教你合理设计数据表,将优化进行到底 为你的mail server增加SPF记录 马云在《赢在中国》对创业者的经典点评 给不同文件夹设置主题并指定其编码 - wdx2008 - 博客园 在线支付类封装 - wdx2008 - 博客园 SET XACT_ABORT各种用法及显示结果 ASP.NET邮件外发 asp.net Request.ServerVariables 各参数说明集合 - wdx2008 sql清除事务日志命令 js不允许单独打开left.aspx或top.apsx - wdx2008 - 博客园 ASP.NET2.0 TreeView的数据库绑定 - wdx2008 - 博客园 forms验证:怎么验证两种身份? Forms 验证进行角色控制全攻略 收藏的一些ASP.net 2.0资料 開源圖表控件ZedGraph
关于asp.net 2.0的用户、角色管理总结(转) - wdx2008 - 博客园
wdx2008 · 2007-06-18 · via 博客园 - wdx2008

在配置使用Membership或其他的Providers的ASP.NET2.0时一定要设置applicationName属性
原文:
Always set the "applicationName" property when configuring ASP.NET 2.0 Membership and other Providers
作者:Scott Guthrie
翻译:范维肖
注释:对于个别名词保留英文描述或翻译参考。

场景
在本地开发一个asp.net2.0的应用程序时使用了Membership、Roles或Profile特性。你创建了一些新用户,一切都没有问题。

然后把这个程序copy到远程服务器(remote server)上(或者只是移动到你本地服务器上的其他目录)然后运行。由于某种原因,虽然我们能够连接到membership数据库,但是当登陆的时候就会出现错误了,它并不抛出连接错误(connection error),而是提示你像类似的错误:“尝试登陆失败,请重试”(Login attempt unsuccessful, please try again)

原因
这种经常出现的错误的原因是因为membership(或者是roles、profile) provider已经被加入到了程序的web.config里了。但是applicationName属性(attribute)并没有被指定(假设下面的代码的粗体部分布存在地话)

<membership>

            
<providers>

                
<clear/>

                
<add name="AspNetSqlMembershipProvider"

                    type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

                    connectionStringName
="LocalSqlServer"

                    enablePasswordRetrieval
="false"

                    enablePasswordReset
="true"

                    requiresQuestionAndAnswer
="true"

  requiresUniqueEmail
="false"

                    passwordFormat
="Hashed"

                    maxInvalidPasswordAttempts
="5"

                    minRequiredPasswordLength
="7"

                    minRequiredNonalphanumericCharacters
="1"

                    passwordAttemptWindow
="10"

                    passwordStrengthRegularExpression
="" 

                    applicationName
="/" 

                
/>

            
</providers>

      
</membership>

如果applicationName属性没有被配置的话,当往程序的数据库里添加数据的时候,asp.net会使用程序在web服务器里的虚拟目录路径(vroot Path)自动的计算applicationName的值。我们打开aspnetdb数据库,看aspnet_Applications表,就能了解这一行为:


这个表里为每个applicationName存储了一个具有唯一值(unique)的ApplicationID。因为在我们的程序里注册一个用户的时候并没有指定一个applicationName属性,所以它自动的被设置(计算)为/website8(早期建立项目时的名字)

然后,使用membership API创建的用户会与ApplicationID相关联,这样可以找到applicationName(and in turn the applicationName)。【这样做的目的可以让我们的多个应用程序和站点使用同一个数据库】。打开aspnet_Users表就可以可以看到这些。


如果把这个程序配置成运行在/website8虚拟目录下那么一切都没有问题。但是如果它被copy到其他的地方或服务器上并且更换了虚拟路径(比如说“/app1”或更通常被设置的“/”)后,当Membership APIs被使用时他们就“看”不到数据库里已有的用户了——因为他们将会使用一个不同的applicationName去数据库里寻找用户,相应地过滤application_Users表中的用户。这就是为什么会出现上面错误的原因。

如何解决这个问题
最简单的办法是打开ASPNETDB数据库中的aspnet_Users和aspnet_Application表,去“回想”(figure out,因为那时候我们的虚拟目录叫什么名字我们恐怕已经忘了)创建用户和其他数据的时候的程序名称(去aspnet_Application表中查找)

然后打开你的web.config文件,添加一个applicationName属性到provider声明的地方并且给他赋值,例如,下面的代码我们把它设置为在aspnet_Application表存在的/website8:

<membership>

            
<providers>

                
<clear/>

                
<add name="AspNetSqlMembershipProvider"

                    type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

                    connectionStringName
="LocalSqlServer"

                    enablePasswordRetrieval
="false"

                    enablePasswordReset
="true"

                    requiresQuestionAndAnswer
="true"

  requiresUniqueEmail
="false"

                    passwordFormat
="Hashed"

                    maxInvalidPasswordAttempts
="5"

                    minRequiredPasswordLength
="7"

                    minRequiredNonalphanumericCharacters
="1"

                    passwordAttemptWindow
="10"

                    passwordStrengthRegularExpression
="" 

                    applicationName
="/website8" 

                
/>

            
</providers>

      
</membership>

这样程序就能正常的工作,无论我们部署这个程序到那个虚拟目录。

当然,我们还要保证Roles、Profile、WebPartPersonalization或其他的我们配置的providers也要被配置了。

这样我们的程序可以正常工作了。

如果在一开始就避免它:
避免这个问题的最好的办法是始终设置applicationName的值。一个比较好的默认值是使用“/”。