当前位置: 代码迷 >> ASP.NET >> 60分求哪位高手能够教会小弟我forms认证
  详细解决方案

60分求哪位高手能够教会小弟我forms认证

热度:873   发布时间:2013-02-25 00:00:00.0
60分求谁能够教会我forms认证
感觉那个东西有点麻烦,可能是因为我是新手的问题,还不是太懂,比如说现在有2个文本框,一个是输入帐号,另外一个是密码,点登陆以后,froms 认证怎么记录这2个值?还要用session吗?怎么读?还有怎么在webconfig里设置??还有,登陆进去以后页面怎么判断你是否符合身份?感觉很难啊

------解决方案--------------------------------------------------------
探讨
cs代码中
if (AdminManager.CheckLogin(m))
{
string strRedirect = Request["ReturnUrl"];
System.Web.Security.FormsAuthentication.SetAuthCookie("AdminUser", true);
if (strRedirect == null)
Response.Redirect("~/Admin/Index.aspx");
Response.Redirect(strRedirect);
}


------解决方案--------------------------------------------------------
Form验证分为很多方式.
先说下配置
<system.web>
<authentication mode="Forms">
<forms defaultUrl="admin/default.aspx" loginUrl="login.aspx" name="test" path="/" protection="All" timeout="30">
<credentials passwordFormat="MD5">
<user name="" password=""/>
</credentials>
</forms>
</authentication>

指定登录后的页面为 admin/default.aspx,如果直接在地址栏中输入admin/default.aspx会直接转到login.aspx.
接下来,你还要配置admin/default.aspx页面不允许匿名访问.我的做法是指定admin目录下的所有页面,都不允许匿名用户访问.这个配置如果你熟悉的话,可以直接在web.config中修改,如果不允许的话,可以启动 vs中的项目中的asp.net配置程序,来帮助你来配置.

如果你的应用比较简单,就是说用户是依靠你来分配的,那么可以简单的新增user节点,以建立一些用户群.

在验证用户身份时,可以这样
 if(System.Web.Security.FormsAuthentication.Authenticate(Textbox1.Text,Textbox2.Text))
{
// 做登录成功的操作
}

这样在admin/default.aspx中,就可以使用Page.User.Identity.Name 来获取登录的用户名了.

以上,只是Form验证中的一种.

还有一种是配合数据库操作.
还有一种是结合用户和角色的(不是指Membership API)

------解决方案--------------------------------------------------------
XML code
        <!--            通过 <authentication> 节可以配置 ASP.NET 使用的             安全身份验证模式,            以标识传入的用户。         -->        <authorization>            <allow users="*"/>        </authorization>        <authentication mode="Forms">            <forms loginUrl="~/Logon.aspx" protection="All" timeout="30" name=".ASPXAUTH" path="/" requireSSL="false" slidingExpiration="true" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"/>        </authentication>        <!--            如果在执行请求的过程中出现未处理的错误,            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,            开发人员通过该节可以配置            要显示的 html 错误页            以代替错误堆栈跟踪。        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">            <error statusCode="403" redirect="NoAccess.htm" />            <error statusCode="404" redirect="FileNotFound.htm" />        </customErrors>        -->    </system.web>    <location path="admin"><!--这里是要登录才能操作的页面文件夹名字-->        <system.web>            <authorization>                <deny users="?"/>                <allow users="*"/>            </authorization>        </system.web>    </location></configuration>
------解决方案--------------------------------------------------------
获取当前用户名 
C# code
string name=Page.User.Identity.Name
------解决方案--------------------------------------------------------
http://blog.csdn.net/eyu777/archive/2008/05/27/2487311.aspx
给你参考下吧 Forms验证,多角色,多登录页
  相关解决方案