如何设置asp.net身份验证的性能 [英] How to set asp.net authenticated properties
本文介绍了如何设置asp.net身份验证的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的web.config文件中进行以下设置。它基本上限制访问,如果用户没有登录的页面。如果我不想使用ASP登录控件或实行会员供应商,我怎么能'告诉'ASP的页面loginregister.aspx已授权请求如果我想实现自己的登录系统?
感谢。
<身份验证模式=表格>
<形式loginUrl =〜/ loginregister.aspx
NAME =。ASPXFORMSAUTH/>
< /认证>
<授权>
<拒绝用户=? />
< /授权>
<位置路径=〜/ secretpage.aspx有的allowOverride =真>
<&的System.Web GT;
<编译调试=真/>
<授权>
<拒绝用户=? />
< /授权>
< /system.web>
< /地点>
解决方案
在您验证的用户,设置了票.. ..
Response.Cookies.Add(TicketHelper.CreateAuthCookie(Login1.UserName,用户数据,Login1.RememberMeSet / *永久性Cookie * /));
使用这个辅助类...
如果使用登录控制,做到在经过身份验证的事件处理程序
使用系统;
使用的System.Web;
使用System.Web.Security;
命名空间CustomAuthRepurposingFormsAuth
{
公共静态类TicketHelper
{
///<总结>
///
///< /总结>
///< PARAM NAME =username的>< /参数>
///< PARAM NAME =用户数据>铭记饼干大小,或者你会追鬼< /参数>
///< PARAM NAME =老大难>< /参数>
///<&回报GT;< /回报>
公共静态的HttpCookie CreateAuthCookie(用户名字符串,字符串用户数据,布尔持久)
{
发出= DateTime.Now日期时间;
// formsAuth不公开超时!?要破解
//宠坏部分周围,继续前进..
的HttpCookie fooCookie = FormsAuthentication.GetAuthCookie(富,真正的);
INT formsTimeout = Convert.ToInt32((fooCookie.Expires - DateTime.Now).TotalMinutes);
日期时间过期= DateTime.Now.AddMinutes(formsTimeout);
串cookiePath = FormsAuthentication.FormsCookiePath;
变种票=新的FormsAuthenticationTicket(0,用户名,发行,到期,真实,用户数据,cookiePath);
返回CreateAuthCookie(门票,有效期,持久性的);
}
公共静态的HttpCookie CreateAuthCookie(门票的FormsAuthenticationTicket,日期到期后,布尔持久)
{
串creamyFilling = FormsAuthentication.Encrypt(票);
变种的cookie =新的HttpCookie(FormsAuthentication.FormsCookieName,creamyFilling)
{
区= FormsAuthentication.CookieDomain,
路径= FormsAuthentication.FormsCookiePath
};
如果(永久)
{
cookie.Expires =到期;
}
返回的cookie;
}
}
I have the following settings in my web.config file. It basically restricts access to a page if the user is not logged in. If I don't want to use the asp login controls or implement a membership provider, how can I 'tell' asp that the loginregister.aspx page has authorized the request if I want to implement my own login system?
Thanks.
<authentication mode="Forms">
<forms loginUrl="~/loginregister.aspx"
name=".ASPXFORMSAUTH" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
<location path="~/secretpage.aspx" allowOverride="true">
<system.web>
<compilation debug="true" />
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
解决方案
After you validate your user, set a ticket....
Response.Cookies.Add(TicketHelper.CreateAuthCookie(Login1.UserName, userData, Login1.RememberMeSet /*persistent cookie*/));
using this helper class...
If using a login control, do it in Authenticated event handler.
using System;
using System.Web;
using System.Web.Security;
namespace CustomAuthRepurposingFormsAuth
{
public static class TicketHelper
{
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="userData">be mindful of the cookie size or you will be chasing ghosts</param>
/// <param name="persistent"></param>
/// <returns></returns>
public static HttpCookie CreateAuthCookie(string userName, string userData, bool persistent)
{
DateTime issued = DateTime.Now;
// formsAuth does not expose timeout!? have to hack around the
// spoiled parts and keep moving..
HttpCookie fooCookie = FormsAuthentication.GetAuthCookie("foo", true);
int formsTimeout = Convert.ToInt32((fooCookie.Expires - DateTime.Now).TotalMinutes);
DateTime expiration = DateTime.Now.AddMinutes(formsTimeout);
string cookiePath = FormsAuthentication.FormsCookiePath;
var ticket = new FormsAuthenticationTicket(0, userName, issued, expiration, true, userData, cookiePath);
return CreateAuthCookie(ticket, expiration, persistent);
}
public static HttpCookie CreateAuthCookie(FormsAuthenticationTicket ticket, DateTime expiration, bool persistent)
{
string creamyFilling = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, creamyFilling)
{
Domain = FormsAuthentication.CookieDomain,
Path = FormsAuthentication.FormsCookiePath
};
if (persistent)
{
cookie.Expires = expiration;
}
return cookie;
}
}
这篇关于如何设置asp.net身份验证的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文