Tagged: MVC RSS Toggle Comment Threads | Keyboard Shortcuts

  • John 1:03 am on January 4, 2012 Permalink | Reply
    Tags: , , MVC, MVC3   

    MVC Update User Online via Action Filter 

    Action filter to update the currently logged in users last online time to allow you to identify which of your users are online

    /// <summary>
    /// User Online Action Filter Attribute
    /// </summary>
    public class UserOnlineActionFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Called by the ASP.NET MVC framework after the action method executes.
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                if (filterContext.HttpContext != null && filterContext.HttpContext.User != null && filterContext.HttpContext.User.Identity != null)
                {
                    Membership.Provider.GetUser(filterContext.HttpContext.User.Identity.Name, true);
                }
            }
            catch
            {
                // BAD: Swallow the exception to prevent it messing with the users action
            }
    
            base.OnActionExecuted(filterContext);
        }
    }
    

    Add the action filter to the global list inside the global.asax Application_Start.

    GlobalFilters.Filters.Add(new UserOnlineActionFilterAttribute());
    
     
  • John 11:05 am on March 10, 2011 Permalink | Reply
    Tags: , IIS Express, , MVC   

    IIS7 & IIS Express 401 Access is denied due to invalid credentials Issue 

    I have been working on a large web application using MVC3, NHibernate using IIS Express to debug and IIS7 to stage. I’m using the membership services provided by ASP.NET to handle user authentication and provide security to the system. However I kept running across an issue, when accessing remotely, that was preventing me from logging in. Whenever I tried to view a page as a non logged in user I was returned a 404 Unauthorized error.

    401 - Unauthorized: Access is denied due to invalid credentials.

    401 - Unauthorized: Access is denied due to invalid credentials.

    After searching around for a solution to why this was not displaying the correct page, even if it was redirecting to the login page I decided to try to set the error displaying to return more information. After adding <httpErrors errorMode=”Details” /> to try and get a detail output from IIS the page started to work as expected.

    With this option enabled the login page was now correctly displayed. This can be set without having to edit the web.config on IIS7 by editing the “Error Pages” features settings for your site in IIS. Set the Error Response to “Detailed errors”

    Related Reading


     
    • John 3:53 pm on March 22, 2011 Permalink | Reply

      Thanks for this post! I ran into the same problem. This seems a little odd to me, have you figured out why MVC3 is exhibiting this behavior?

      • John 4:13 pm on March 22, 2011 Permalink | Reply

        Figured out what is going on… this serverfault answer is the perfect explanation:

        http://serverfault.com/questions/137073/401-unauthorized-on-server-2008-r2-iis-7-5

        In my case, I had a logon page calling Html.RenderAction(), and that action had an [Authorization] attribute.

        Cheers.

        • John 4:17 pm on March 22, 2011 Permalink | Reply

          Sorry… [Authorize] :-)

          • John 8:24 pm on March 22, 2011 Permalink | Reply

            Cheers for the update, I’ll have to take a closer look I also have numerous Html.RenderAction() that properly implemnt the [Authorize] attribute.

            However I only noticed it since MVC3 RC, I don’t think it was in either of the previous of the beta versions.

    • erick 3:31 am on August 22, 2011 Permalink | Reply

      thx alot, it help me alot..i try to figure this out for 6 days :(

    • Ryan Faricy 5:44 am on December 14, 2011 Permalink | Reply

      I can confirm that a path leading to an eventual [Authorize] attribute causes this – removing the RenderAction or allowing that action to render without [Authorize] fixes it.

  • John 7:21 pm on June 28, 2009 Permalink | Reply
    Tags: , , MVC,   

    Professional ASP.NET MVC 1.0 

    At work we have been looking at the ASP.NET MVC, model-view-controller, framework. After looking around at various different tutorials and articles we found a sample chapter from the wrox book Professional ASP.NET MVC 1.0. The sample chapter took the hello world example and create a full application. The application, NerdDinner is an application that allows you to create dinner parties and RSPV to them. The sample covers a lot of the functionality that is within ASP.NET MVC. The sample chapter can be found online at Wrox’s website with the source code hosted at codeplex.


    Professional ASP.NET MVC 1.0

    Reading though the sample chapter is a good way to get started with ASP.NET MVC as the chapter explains what it is doing in a easy to understand manor, but more importantly it explains why they are being done. The chapter is a comprehensive walk through of how to create the NerdDinner application. Throughout the chapter the authors provide titbits of useful information, for example the use of the repository pattern and dependency injection. Throughout the chapter the authors trying to enforce good coding practice and often make reference to test driven development which in a later chapter they go into more detail about, discussing different development techniques.

    The sample chapter is more than enough to get anyone started with ASP.NET MVC and follows the development process from start to finish including a section on unit testing the application. The following chapters delve into more detail about specific areas of the ASP.NET MVC framework. The second and third chapters go into detail about the MVC pattern, detailing its background and its use on the web. The chapter also provides an overview, albeit brief, of other frameworks that are available. Although their coverage was brief they were not mentioned as either superior or inferior rather just as alternatives to the ASP.NET MVC framework. This tone was subsequently repeated in the third chapter when the authors compare ASP.NET with WebForms to ASP.NET MVC. They often mention that MVC is not a replacement for WebForms rather it is a different approach. These two chapters are extremely useful as they allow you to make an informed decision whether ASP.NET MVC is the right choice for you.

    I’ve still to finish reading the rest of this book so I’ll update this post the more I read of it.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel