Print Search
帖子排序:    
帖子发起人: scienceMode   发起时间: 2005-08-22 22:22 下午   回复: 4
bddtg 离线,最后访问时间: 2005-8-23 12:08:19 scienceMode

发帖数前100位
注册: 2005-08-22
发 贴: 2
Crying [:'(] 问,谁对基于forms的角色认证很了解
 2005-08-22, 22:22 下午

我为什么总是不能跳过登录界面?

FormsAuthenticationTicket authTicket = new
     FormsAuthenticationTicket(1,                          // version
     txtUserName.Text,           // user name
     DateTime.Now,               // creation
     DateTime.Now.AddMinutes(60),// Expiration
     true,                      // Persistent
     roles );

 

,我把Persistent想改为true,也不行,谁能解答?

IP 地址: 已记录   报告
stone 离线,最后访问时间: 2007-9-27 15:59:25 Stone Tao



发帖数前25位
注册: 2005-07-24
发 贴: 45
Re: 问,谁对基于forms的角色认证很了解
 2005-08-23, 11:07 上午

你在Config中配置了吗?以下摘自MSDN

配置 ASP.NET 身份验证支持。该元素只能在计算机、站点或应用程序级别声明。如果试图在配置文件中的子目录或页级别上进行声明,则将产生分析器错误信息。

<configuration>
   <system.web>
      <authentication>

<authentication mode="Windows|Forms|Passport|None">

   <forms name="name"

          loginUrl="url"

          protection="All|None|Encryption|Validation"

          timeout="30" path="/" >

          requireSSL="true|false"

          slidingExpiration="true|false">

      <credentials passwordFormat="Clear|SHA1|MD5">

         <user name="username" password="password"/>

      </credentials>

   </forms>

   <passport redirectUrl="internal"/>

</authentication>

 

必选属性

属性

选项

说明

mode

 

控制应用程序的默认身份验证模式。

 

Windows

Windows 验证指定为默认的身份验证模式。当使用以下任意形式的 Microsoft Internet 信息服务 (IIS) 身份验证时使用该模式:基本、简要、集成的 Windows 验证 (NTLM/Kerberos) 或证书。

 

Forms

ASP.NET 基于窗体的身份验证指定为默认的身份验证模式。

 

Passport

Microsoft Passport 身份验证指定为默认的身份验证模式。

 

None

不指定任何身份验证。只有匿名用户是预期的或者应用程序可以处理事件以提供其自身的身份验证。

 

子标记

子标记

说明

<forms>

为基于窗体的自定义身份验证配置 ASP.NET 应用程序。

<passport>

指定要重定向到的页(如果该页要求身份验证,而用户尚未通过 Passport 注册)。

 

示例

以下示例为基于窗体的身份验证配置站点,指定传输来自客户端的登录信息的 Cookie 的名称,并指定当初始身份验证失败时所使用的登录页的名称。必须将 <authorization> 节包含在内才能要求所有用户进行 Forms 身份验证,并且拒绝对站点的所有匿名用户访问。

<configuration>

   <system.web>

      <authentication mode="Forms">

         <forms name="401kApp" loginUrl="/login.aspx"/>

      </authentication>

      <authorization>

         <deny users="?"/>

      </authorization>

   </system.web>

</configuration>

 

 

 

IP 地址: 已记录   报告
bddtg 离线,最后访问时间: 2005-8-23 12:08:19 scienceMode

发帖数前100位
注册: 2005-08-22
发 贴: 2
Re: 问,谁对基于forms的角色认证很了解
 2005-08-23, 12:08 下午
http://www.microsoft.com/china/msdn/library/architecture/architecture/architecturetopic/BuildSucApp/BSSAsecmodsecmod18.mspx
我测试了这个程序,完成后无法让程序跳过登录环节。
IP 地址: 已记录   报告
stone 离线,最后访问时间: 2007-9-27 15:59:25 Stone Tao



发帖数前25位
注册: 2005-07-24
发 贴: 45
Re: 问,谁对基于forms的角色认证很了解

附件: GenericPrincipalApp.rar
 2005-08-23, 20:50 下午

MSDN上面的例子有点小问题.他存储用户登录信息是用加密Cookies来做的.需要以下地方修改代码:

private void Button1_Click(object sender, System.EventArgs e)
  {
   bool isAuthenticated = IsAuthenticated( txtUserName.Text,txtPassword.Text );
   if (isAuthenticated == true )
   {
    string roles = GetRoles( txtUserName.Text, txtPassword.Text );
    FormsAuthenticationTicket authTicket = new
     FormsAuthenticationTicket(1,                          // version
     txtUserName.Text,           // user name
     DateTime.Now,               // creation
     DateTime.Now.AddMinutes(60),// Expiration
     false,                      // Persistent
     roles );                    // User data

    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
    // Create a cookie and add the encrypted ticket to the
    // cookie as data.
    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
     

     Response.Cookies.Add(authCookie);//MSDN上面没有写这一句,加上就可以了!

    Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserName.Text, false ));
   }
  }

我已经将修改好的例子上传,你可以参照一下!

IP 地址: 已记录   报告
zyyang 离线,最后访问时间: 2008-8-6 9:30:09 Daniel.Yang



发帖数前10位
注册: 2005-07-24
发 贴: 300
Re: 问,谁对基于forms的角色认证很了解
 2005-08-24, 14:12 下午
靠.
这么重要的一句都能忘记.
真不知道MSDN是故意的还是....
IP 地址: 已记录   报告
合肥微软技术中心社区 » 技术讨论区 » .NET技术相关 » ASP.NET » 问,谁对基于forms的角色认证很了解

Powered by Community Server Powered by CnForums.Net