Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hey,

I asked a similar question a couple of days ago, but I don't see the same effect as described.

Essentially, I use ASP with form authentication. I manage users with a membership provider, but it has been pretty extensively customized.

It is possible that a user might have permissions removed whilst they are logged in. I was to kick them out asap.

I use signalr in my apps, and I now check the user permissions on each and every hub query. Authorization failure caused the client to navigate to a page that logs them out (with a harshly worded message letting them know that they've been kicked out).

I had assumed, and was informed that, when the user navigates to a new page after losing authorization that asp would check the users permissions again. It doesn't.

Do I have to add checks into each page or is there a setting I'm missing?

Any advice is welcome ^_^

Thanks
Andy

What I have tried:

I use Signalr for SPAs which checks authorisation so in most cases the effect is there for my apps, but my colleagues mostly use page navigation to control workflow and don't use SignalR.

We can check authorisation on every page load, but I has heard that this is done automagically. Is there a setting or a simple app wide fix?
Posted
Updated 14-Sep-16 2:35am

1 solution

With forms authentication, the only thing that's validated for each request is that the cookie contains a valid forms authentication ticket, and that the ticket has not expired.

If you want to check that the ticket still represents a valid user in your membership store, then you need to add that check to each request. The simplest solution is probably to use a custom IHttpModule:
C#
public sealed class MembershipValidationModule : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        if (context == null) throw new ArgumentNullException(nameof(context));

        context.PostAuthenticateRequest += (s, e) =>
        {
            var app = s as HttpApplication;
            if (app?.Context != null)
            {
                OnAuthenticated(new HttpContextWrapper(app.Context));
            }
        };
    }

    private static void OnAuthenticated([NotNull] HttpContextBase context)
    {
        if (IsFormsAuthenticated(context.User))
        {
            var user = Membership.GetUser();
            if (user == null || !user.IsApproved)
            {
                FormsAuthentication.SignOut();
                context.User = null;
            }
        }
    }

    private static bool IsFormsAuthenticated(IPrincipal user)
    {
        if (user == null || !user.Identity.IsAuthenticated) return false;
        return string.Equals(user.Identity.AuthenticationType, "Forms", StringComparison.OrdinalIgnoreCase);
    }
}

XML
<configuration>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        
        <modules>
            <add
                name="MembershipValidationModule"
                preCondition="managedHandler"
                type="YourNamespace.MembershipValidationModule, YourAssembly"
            />
        </modules>
    </system.webServer>
</configuration>
 
Share this answer
 
Comments
Andy Lanng 14-Sep-16 9:14am    
Awesome! Thanks ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900