



















const string baseResourceFile = "ResourceLanguage";
Assembly primaryResource = Assembly.Load(baseResourceFile);
Application["RM"] = new ResourceManager(baseResourceFile, primaryResource);
public class Root
{
public Root()
{
}
private static ResourceManager _rm;
public static ResourceManager rm
{
get
{
if(_rm==null)
_rm = (ResourceManager) HttpContext.Current.Application["RM"];
return _rm;
}
}
}
Root.rm.GetString("Lgntxt4");
<authentication mode="Forms">
<forms name=".CRM" protection="All" timeout="60" loginUrl="login.aspx" slidingExpiration="true" />
</authentication>
<configuration>
<system.web>
<authorization>
<allow roles="SystemAdministrator,MembershipAdministrator" />
<deny users="*" />
</authorization>
</system.web>
<location path="ShowAllUsers.aspx">
<system.web>
<authorization>
<allow roles="SystemAdministrator,MembershipAdministrator" users="?" />
</authorization>
</system.web>
</location>
</configuration>
public FormsAuthenticationTicket(
int version, //设为1
string name, //用户标示
DateTime issueDate, //Cookie 的发出时间, 设置为 DateTime.Now
DateTime expiration, //过期时间
bool isPersistent, //是否持久性(根据需要设置,若是设置为持久性,在发出
cookie时,cookie的Expires设置一定要设置)
string userData, //这里用上面准备好的用逗号分割的role字符串
string cookiePath // 设为"/",这要同发出cookie的路径一致,因为刷新cookie
要用这个路径
);
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket (1,"kent",DateTime.Now, DateTime.Now.AddMinutes(30), false,UserRoles,"/") ;
//将身份验证票加密序列化成一个字符串
string HashTicket = FormsAuthentication.Encrypt (Ticket) ;
//生成cookie
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket) ;
protected void Application_AuthorizeRequest(object sender, System.EventArgs e)
{
HttpApplication App = (HttpApplication) sender;
HttpContext Ctx = App.Context ; //获取本次Http请求相关的HttpContext对象
if (Ctx.Request.IsAuthenticated == true) //验证过的用户才进行role的处理
{
FormsIdentity Id = (FormsIdentity)Ctx.User.Identity ;
FormsAuthenticationTicket Ticket = Id.Ticket ; //取得身份验证票
string[] Roles = Ticket.UserData.Split (',') ; //将身份验证票中的role数据转成字符串数组
Ctx.User = new GenericPrincipal (Id, Roles) ; //将原有的Identity加上角色信息新建一个GenericPrincipal表示当前用户,这样当前用户就拥有了role信息
}
}
protected override object LoadPageStateFromPersistenceMedium()
{
if (_formatter == null)
{
_formatter = new LosFormatter();
}
string str = Request.Form["__VIEWSTATE_KEY"];
if (!str.StartsWith("VIEWSTATE#"))
{
throw new Exception("Invalid viewstate key:" + str);
}
#if VIEWSTATETOSQLSERVER
//从数据库中获取ViewState,参数为SessionID
db.GetViewState(a);
//操作失败则跳转到上一页面
//返回序列化后的viewstate
return _formatter.Deserialize(viewState);
#else
if (!str.StartsWith("VIEWSTATE#"))
{
throw new Exception("Invalid viewstate key:" + str);
}
return Cache[str];
#endif
}
protected override void SavePageStateToPersistenceMedium(object viewState)
{
string str = "VIEWSTATE#" + Session.SessionID.ToString() + "#" + DateTime.Now.Ticks.ToString();
//自动注册隐藏字段
RegisterHiddenField("__VIEWSTATE_KEY", str);
RegisterHiddenField("__VIEWSTATE", "");
if (_formatter == null)
{
_formatter = new LosFormatter();
}
#if VIEWSTATETOSQLSERVER
StringWriter _writer = new StringWriter();
_formatter.Serialize(_writer, viewState);
//$$$$$$$$$此处插入将viewstate写入数据库,需要提供参数:Request.Path,用户ID,SessionID,ViewState$$$$$$$$$
//如果操作失败,则从数据库中删除sessionID对应的所以记录
db.InsertSession(a,b,c,d);
#else
Cache.Add(str, viewState, null, DateTime.Now.AddMinutes(Session.Timeout), TimeSpan.Zero, CacheItemPriority.Default, null);
#endif
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。