+ Reply to Thread
Page 3 of 5 FirstFirst 1 2 3 4 5 LastLast
Results 21 to 30 of 47

Thread: Throw HTTP Status Code error

  1. #21
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Nope...Get the same error...I've also tried:-
    Code:
    HttpContext context = application.Context;
    
    And
    Code:
    application.Context.User...
    
    But all give the same error...

  2. #22
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    What if you add the following to the top of the page?
    Code:
    using System;
    using System.Web;
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #23
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by jmurrayhead View Post
    What if you add the following to the top of the page?
    Code:
    using System;
    using System.Web;
    
    I already have those J...Damn this is annoying...

    Surely this should be reasonably straightforward...If you're using authentication, you would want the option of returning HTTP status codes, wouldn't you?

  4. #24
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Are you building this httpmodule as its own project or as part of your application? You may need to add a reference for System and System.Web
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #25
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    It's part of the application.

    I'm just trying to work through this logically. I'll post the whole code:-
    Code:
    using System;
    using System.Web;
    using System.Net;
    
    using MyApp.App.BLL.Users;
    
    namespace MyApp.App.BLL.Permissions
    {
    
        public class PermissionModule : IHttpModule
        {
            public PermissionModule()
            {
            }
            public void Init(HttpApplication application)
            {
                application.BeginRequest +=
                    (new EventHandler(this.Application_BeginRequest));
                application.EndRequest +=
                    (new EventHandler(this.Application_EndRequest));
            }
    
            private User user { get; set; }
    
            public void Application_BeginRequest(Object source, EventArgs e)
            {
                // Create HttpApplication and HttpContext objects to access
                // request and response properties.
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
                if (user == null)
                {
                    user = new User();
                    user = User.GetUser(Convert.ToInt32(context.User.Identity.Name));
                }
    
                SiteMapNode node = SiteMap.CurrentNode;
                if (node != null)
                {
                    if (node["section"] != "0")
                    {
                        if (!user.admin && !user.CheckUserPermission(Convert.ToInt32(node["section"]), Convert.ToInt32(node["permission"])))
                            HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        //throw new HttpException(403, "Permission Denied");
                        //HttpContext.Current.Response.Redirect("default.aspx?access=none");
                    }
                }
                /*
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
                string filePath = context.Request.FilePath;
                string fileExtension =
                    VirtualPathUtility.GetExtension(filePath);
                if (fileExtension.Equals(".aspx"))
                {
                    context.Response.Write("<h1><font color=red>" +
                        "HelloWorldModule: Beginning of Request" +
                        "</font></h1><hr>");
                }
                 */
            }
    
            private void Application_EndRequest(Object source, EventArgs e)
            {
                //This isn't doing anything at the moment
                /*
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
                string filePath = context.Request.FilePath;
                string fileExtension =
                    VirtualPathUtility.GetExtension(filePath);
                if (fileExtension.Equals(".aspx"))
                {
                    context.Response.Write("<hr><h1><font color=red>" +
                        "HelloWorldModule: End of Request</font></h1>");
                }
                 */
            }
            public void Dispose() { }
        }
    }
    
    So on initializing, it adds EventHandlers to BeginRequest and EndRequest events of the application. So when the application starts, Init is the only thing that should be called, right?

    So, what about pages where there wouldn't be an instance? My login page, for example. How would I check for an empty reference?
    Code:
    if(context.User==null)?
    if(context.User.Identity==null)?
    if(context.User.Identity.Name==null)?
    
    Or would any of the those work?

  6. #26
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. I added this conditional to my Begin_Request sub
    Code:
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
                if (context.User != null)
    
    and it's failing each time ie context.User is always coming back as null...

    So, I obviously need to find where this is initialized and check the permission after this event.

  7. #27
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    I think I might know what the problem is. Not sure though.

    This is being called on each request, including CSS, images(I presume) etc., not just on asp.net page requests.

    HttpContext.Current is an object because in the else of my if(name==null), I have
    Code:
                else
                {
                    HttpContext.Current.Response.Write("<h1><font color=red>" +
                        "PermissionModule: Beginning of Request" +
                        "</font></h1>");
                }
    
    And that gets written to each request (including the CSS files).
    I tried the code from here but my name is still coming back as null.

  8. #28
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. The HttpContext User is only available after AuthorizeRequest. However, I changed my code to check the permissions in an event handler for AuthorizeRequest and it's acting the same as if I used it in my master page.

    It allows a user to view a page they shouldn't have permission for. I've used this to send the status code so I don't know if it's this that is the problem.
    Code:
    HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Forbidden;
    
    Might just try Fiddler to see if what gets returned. See if it is a 403 status code.

  9. #29
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Ok...I've used this code
    Code:
            private void Application_AuthenticateRequest(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;
                HttpResponse response = application.Context.Response;
                string name = application.Context.User.Identity.Name;
                if (name != null)
                {
                    if (user == null)
                    {
                        user = new User();
                        user = User.GetUser(Convert.ToInt32(name));
                    }
    
                    SiteMapNode node = SiteMap.CurrentNode;
                    if (node != null)
                    {
                        if (node["section"] != "0")
                        {
                            if (!user.admin && !user.CheckUserPermission(Convert.ToInt32(node["section"]), Convert.ToInt32(node["permission"])))
                                response.StatusCode = 403;
                            HttpContext.Current.Response.Write("<h1><font color=red>" + response.StatusCode +
                                "PermissionModule Node: Beginning of Request" +
                                "</font></h1>");
                            //HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                            //throw new HttpException(403, "Permission Denied");
                            //HttpContext.Current.Response.Redirect("default.aspx?access=none");
                        }
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write("<h1><font color=red>" + response.StatusCode +
                        "PermissionModule: Beginning of Request" +
                        "</font></h1>");
                }
            }
    
    and response.StatusCode is returning 403. Why isn't this then showing a "You do not have permission..." error page?

  10. #30
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    What do you see?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


+ Reply to Thread
Page 3 of 5 FirstFirst 1 2 3 4 5 LastLast

Similar Threads

  1. http://conception-2-school.com/forums/
    By jmurrayhead in forum Website Reviews
    Replies: 15
    Last Post: April 22nd, 2009, 01:11 PM
  2. Need PHP/SQL code help...to fix written code
    By honeybeeaz in forum PHP Development
    Replies: 10
    Last Post: November 11th, 2008, 03:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO