存储用户信息/传递web.config身份验证

时间:2009-10-02 03:03:51

标签: .net authentication login web-config

我正在尝试使用一些简单的身份验证编写一个简单的内部应用程序。我也试图通过web.config快速了解表单身份验证。

如果我将“用户名”和“密码”硬编码到C#代码中并执行简单的条件操作,那么我的身份验证工作正常。

但是,我很难在web.config文件中存储要检查的用户/通行证。

MSDN手册说将其放入web.config:

<authentication mode="Forms">
    <forms loginUrl="login.aspx">
        <credentials passwordFormat="SHA1">
            <user name="user1" password="27CE4CA7FBF00685AF2F617E3F5BBCAFF7B7403C" />
            <user name="user2" password="D108F80936F78DFDD333141EBC985B0233A30C7A" />
            <user name="user3" password="7BDB09781A3F23885CD43177C0508B375CB1B7E9"/>
        </credentials>
    </forms>
</authentication>

但是,当我在“身份验证”部分添加“凭据”时,我收到此错误:

Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized element 'credentials'.

Source Error:

Line 44:     <authentication mode="Forms">
Line 45:       <forms loginUrl="login.aspx" />
Line 46:       <credentials>
Line 47:         
Line 48:       </credentials>


Source File: C:\inetpub\wwwroot\asp\projects\passwordCatalog\passwordCatalog\web.config    Line: 46 

所以我的问题是,我将如何以及在哪里添加以下内容在web.config文件中?

<credentials passwordFormat="SHA1">
    <user name="johndoe" password="mypass123" />
</credentials>

2 个答案:

答案 0 :(得分:3)

&lt; credentials&gt; element应该嵌套在forms元素中。

您的错误消息表明情况并非如此:您已关闭第45行的表单元素(&lt; forms ... /&gt; 而不是&lt;表单。 ..&gt;

Line 44:     <authentication mode="Forms">
Line 45:       <forms loginUrl="login.aspx" />
Line 46:       <credentials>
Line 47:         
Line 48:       </credentials>

你想要的是:

<authentication mode="Forms">
    <forms loginUrl="login.aspx">
        <credentials>
        ...
        </credentials>
    </forms>
    ...

答案 1 :(得分:0)

使用passwordFormat="Clear"

<authentication mode="Forms">
   <forms loginUrl="default.aspx">
      <credentials passwordFormat="Clear">
    <user name="user1" password="pass1"/>
      </credentials>
   </forms>
</authentication>
相关问题