Skip to content

mdufourneaud/Nancy.MSOwinSecurity

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Nancy.MSOwinSecurity

Enables integration with Microsoft's OWIN security middeware from the Katana project. This package will allow you to easily access the IAuthenticationManager. .NET 4.5 only.

How to use

Installing the nuget package:

install-package Nancy.MSOwinSecurity

Getting the authentication manager and current user from the context:

public class MyModule : NancyModule
{
    public MyModule()
    {
        Get["/"] = _ =>
        {
            IAuthenticationManager authenticationManager = Context.GetAuthenticationManager();
            //ClaimsPrincipal user = authenticationManager.User;
            //authenticationManager.SignIn(..);
            //authenticationManager.SignOut(..);
            //authenticationManager.AuthenticateAsync(..);
            //authenticationManager.Challenge(..);
        };
    }
}

Securing a module:

public class MyModule : NancyModule
{
    public MyModule()
    {
        this.RequiresMSOwinAuthentication();
        Get["/"] = _ => {...}
    }
}

Securing a route:

public class MyModule : NancyModule
{
    public MyModule()
    {
        
        Get["/"] = _ => 
        {
            this.RequiresMSOwinAuthentication();
            ....
        }
    }
}

Getting the current user (just a helper extension around IAuthenticationManager.User):

public class MyModule : NancyModule
{
    public MyModule()
    {
        
        Get["/"] = _ => 
        {
            ClaimsPrincipal = Context.GetMSOwinUser();
            ....
        }
    }
}

Authorizing the user at module level:

public class MyModule : NancyModule
{
    public MyModule()
    {
        this.RequiresSecurityClaims(claims => claims.Any(
            claim.ClaimType = ClaimTypes.Country &&
            claim.Value.Equals("IE", StringComparision.Ordinal)));
        Get["/"] = _ => 
        {
           ....
        }
    }
}

Authorizing the user at route level:

public class MyModule : NancyModule
{
    public MyModule()
    {
        
        Get["/"] = _ => 
        {
            this.RequiresSecurityClaims(claims => claims.Any(
                claim.ClaimType = ClaimTypes.Country &&
                claim.Value.Equals("IE", StringComparision.Ordinal)));
            ...
        }
    }
}

Personal note: this nancy extension package would integrate much better if we had extenstion properties in c# :(

License

MIT

Please report issues on github. Questions, criticisms, compliments or otherwise, @randompunter, or pop by the Nancy Jabbr room.

About

Nancy Owin Security integration

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%