Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Learning Ruby on windows: step 0fluent security and making it work in VB.Net »
    comments

    Introduction

    So after solving the little bug with regards to fluent security and VB.Net I now can try out fluent security.

    How it works

    So now that I have it working I needed to find out how it works. My first attempt was to make the About Function in the Homecontroller only accessible for logged in users. Yes I know this seems illogical but who cares about that anyway.

    For this I need to change the configuration code in my Global.asax to this.

    1. SecurityConfigurator.Configure(Sub(configuration)
    2.                                            configuration.GetAuthenticationStatusFrom(Function() HttpContext.Current.User.Identity.IsAuthenticated)
    3.                                            configuration.For(Of HomeController)().Ignore()
    4.                                            configuration.For(Of HomeController)(Function(x) x.About).DenyAnonymousAccess()
    5.  
    6.                                            configuration.For(Of AccountController)().DenyAuthenticatedAccess()
    7.                                            configuration.For(Of AccountController)(Function(x) x.LogOff()).DenyAnonymousAccess()
    8.                                        End Sub)
    9.  
    10.         GlobalFilters.Filters.Add(New HandleSecurityAttribute(), 0)

    Do you see the first line of the HomeController configuration that says ignore? That means that all functions in Homecontroller will have no security rules so everyone can use them. As with any good policy we can however override this and we do this in the next line, where we say that the About function must Deny Anonymous access. Which is what we want. In short the Index function of our standard HomeController will be accessible for everyone and About just for logged in users.

    When our users see this and click the about button/link.

    they will get this.

    Neither are ideal situations but we now know what happens.

    You get this exeptions that your users should never see because that is the default. Just go look at the code.

    1. using System.Web.Mvc;
    2.  
    3. namespace FluentSecurity
    4. {
    5.     public class ExceptionPolicyViolationHandler : IPolicyViolationHandler
    6.     {
    7.         public ActionResult Handle(PolicyViolationException exception)
    8.         {
    9.             throw exception;
    10.         }
    11.     }
    12. }

    You can now also conclude that this behaviour is overridable. Which it is.

    We will however need structuremap or another IoC container. So I created a new handler with the correct name.

    1. Namespace Security
    2.     Public Class DenyAnonymousAccessPolicyViolationHandler
    3.         Implements FluentSecurity.IPolicyViolationHandler
    4.  
    5.  
    6.         Public Function Handle(ByVal exception As FluentSecurity.PolicyViolationException) As System.Web.Mvc.ActionResult Implements FluentSecurity.IPolicyViolationHandler.Handle
    7.             Return New HttpUnauthorizedResult(exception.Message)
    8.         End Function
    9.  
    10.     End Class
    11. End Namespace

    And I change my global.asax to this after adding structuremap via nuget.

    1. ObjectFactory.Configure(Sub(x) x.For(Of IPolicyViolationHandler).Add(Of Security.DenyAnonymousAccessPolicyViolationHandler)())
    2.  
    3.         SecurityConfigurator.Configure(Sub(configuration)
    4.                                            configuration.GetAuthenticationStatusFrom(Function() HttpContext.Current.User.Identity.IsAuthenticated)
    5.                                            configuration.ResolveServicesUsing(Function(type) ObjectFactory.GetAllInstances(type).Cast(Of Object)())
    6.                                            configuration.For(Of HomeController)().Ignore()
    7.                                            configuration.For(Of HomeController)(Function(x) x.About).DenyAnonymousAccess()
    8.  
    9.                                            configuration.For(Of AccountController)().DenyAuthenticatedAccess()
    10.                                            configuration.For(Of AccountController)(Function(x) x.LogOff()).DenyAnonymousAccess()
    11.                                        End Sub)
    12.  
    13.         GlobalFilters.Filters.Add(New HandleSecurityAttribute(), 0)

    And now I get the logon screen when I click on about.

    Conclusion

    It works, what can I say. You can now easily add security to your asp.net MVC 3 application without having to add attributes all over the place.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    1 comment

    Comment from: Mehdi [Visitor]
    Mehdi Thanks, I was indeed helpful for me as I was confused how to configure 'DenyAnonymousAccessPolicyViolationHandler'.
    01/08/12 @ 17:42

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)