如何实施表格认证 [英] How to implement Form Authentication
问题描述
我正在设计一个包含其他页面的登录页面.当用户尝试在登录之前访问另一个页面时,
他应在输入登录详细信息后提示登录页面用户应重定向到请求的页面.登录详细信息应对照数据库值进行检查.我想应用我尝试过的表单身份验证,但页面未重定向.请指定问题的解决方案.
更改主题.-SureshSuthar [/编辑]
I am designing a login page with some other pages.when the user try to access the other page before login
he should prompt to login page after entereing the login details user should redirected to requested page.login details should be checked against database values .i want to apply form authentication i have tried but page is not redirected.please specify the solution to the problem.
Change Subject.-Suresh Suthar[/Edit]
推荐答案
朋友您好,这将帮助您...
http://www.asp.net/security/tutorials/an-overview- of-forms-authentication-vb [ ^ ]
Hello friend this will help you...
http://www.asp.net/security/tutorials/an-overview-of-forms-authentication-vb[^]
使用表单身份验证:
Use forms Authentication:
<authentication mode="Forms">
<forms loginurl="Login.aspx" defaulturl="Default.aspx" path="/" protection="All" timeout="30" />
</authentication>
在web.config中定义授权:
Define Authorization in web.config:
<authorization>
<deny users="?" />
</authorization>
如果您希望匿名用户访问特定页面,请尝试添加位置,
If you want anonymous user to access particular page then try adding locations,
<location path="Join.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
在web.Config中,您需要更改一些值.
In web.Config you need to change some values.
<authentication mode="Forms">
<forms loginurl="Login.aspx" defaulturl="Home.aspx" path="/">
</forms></authentication>
<authorization>
<deny users="?" />
</authorization>
在Login.aspx.cs页面中,您需要通过cookie对用户进行身份验证
In Login.aspx.cs Page you need to authenticate the user by cookies
// Your method to auth User
if (IsAuth)
{
// Create the authentication ticket and store the roles in the custom UserData property of the authentication ticket
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(1, // version
txtUserId.Value, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(1),// Expiration
false, // Persistent
userData, // User data
FormsAuthentication.FormsCookiePath);
// Now encrypt the ticket.
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);
// Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);
// Redirect the user to the originally requested page
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserId.Value, true));
}
这篇关于如何实施表格认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!