给出一些我们可以在每个Application事件中执行的示例,例如Application_AuthenticateRequest [英] Give some Examples That we can do in each Application events like Application_AuthenticateRequest

查看:95
本文介绍了给出一些我们可以在每个Application事件中执行的示例,例如Application_AuthenticateRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void Application_BeginRequest(object sender, EventArgs e)
       {
           // Code that runs on application startup
           // Check whether to begin the request

       }
       void Application_AuthenticateRequest(object sender, EventArgs e)
       {

           /*This is called just before authentication is performed.
            * You could add your own authentication logic in this method to provide custom authentication*/

       }
       void Application_AuthorizeRequest(object sender, EventArgs e)
       {
           /*This is called when the authentication stage is completed.
            * It's executed at the stage to determine user privileges and permissions.
            * You could add custom code to assign user special privileges.*/

       }
       void Application_ResolveRequestCache(object sender, EventArgs e)
       {
           // Code that runs on application startup

       }
       void Application_AcquireRequestState(object sender, EventArgs e)
       {
           /*This is called just before session-specific data is retrieved for the client
            * and is used to populate Session Collection.*/
       }
       void Application_PreRequestHandlerExecute(object sender, EventArgs e)
       {
           // Code that runs on application startup

       }


       //Handler Now Execute the request



       void Application_PostRequestHandlerExecute(object sender, EventArgs e)
       {


       }
       void Application_ReleaseRequestState(object sender, EventArgs e)
       {
           /* This method is called when the session-specific information is about to be serialized
            * from the Session Collection so that it's available for the next request. */

       }
       void Application_UpdateRequestCache(object sender, EventArgs e)
       {
           //This is called just before information is added to the output cache.
           //If you have enabled output caching for your web page,
           //it's now the time ASP.NET insert the rendered HTML into the cache.


       }

推荐答案

我认为提供的评论非常多是示例 - 但作为一个进一步的暗示怎么样



I think the supplied comments pretty much are the examples - but as a futher hint how about

void Application_Start(object sender, EventArgs e)
{
  // Check database server is up and running
}

void Application_End(object sender, EventArgs e)
{
  // check all file shares etc are closed
}

我想要为每个请求调用的事件示例..



但不是常规事件,如Application_start,Application_End,Application_Disposed
I want Examples for events those called for Every request..

but not regular events like Application_start,Application_End,Application_Disposed


cs文件代码来自数据库的validateadmin首先通过validateadmin()方法

cs file code validateadmin from database through validateadmin() method first
void validateuser(string id, string pass)
   {

       string ob = Request.QueryString["returnurl"];
       if (validateadmin())
       {
           if (ob == null)
           {

               FormsAuthentication.Initialize();
               string strrole = null;
               strrole = "admin";
               FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, userid.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, strrole, FormsAuthentication.FormsCookiePath);
               Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)));
               //loggedmein();
               Response.Redirect("~/Admin/adminMain.aspx");
           }
           else
           {
               FormsAuthentication.RedirectFromLoginPage(userid.Text, false);
           }
       }
       else
       {
           lblmsg.Visible = true;
           lblmsg.Text = "wrong user id or password";
       }


   }







然后



in global.asax






then

in global.asax

protected void Application_AuthenticateRequest(object sender, EventArgs e)
  {
      if (this.Context.Request.IsAuthenticated)
      {
          string str = null;
          if ((HttpContext.Current.User.Identity.Name.ToLower() == "admin"))
          {
              str = "admin";
          }

          else
          {
              str = "usr";
          }
          string[] roles = new string[2];
          roles[0] = str;
          Context.User = new System.Security.Principal.GenericPrincipal(this.Context.User.Identity, roles);
      }
  }




webconfig中的






in webconfig

<appsettings>
    <add key="ConnectionString" value="con" />
    <add key="strrole" value="usr" />
</appsettings>
<system.web>
<customerrors mode="Off" />
    <compilation debug="true" targetframework="4.0" />
<authentication mode="Forms">
  <forms loginUrl="login.aspx" name="FlashUpload" path="/"></forms>
</authentication>
</system.web>


这篇关于给出一些我们可以在每个Application事件中执行的示例,例如Application_AuthenticateRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆