Oauth authentication with owin & Nancy Oauth authentication with owin & Nancy asp.net asp.net

Oauth authentication with owin & Nancy


I'll expand on a comment I was about to leave and just make it an answer (even though you moved away from Nancy it seems). I asked a similar question, and was pointed to the following code example on github:

https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree/dev/samples/Nancy/Nancy.Client

Assuming you have your OIDC wired up properly in Startup.cs, the following code is what I needed to get Nancy module to trigger the authentication on my signin/signout routes:

namespace Nancy.Client.Modules {    public class AuthenticationModule : NancyModule {        public AuthenticationModule() {            Get["/signin"] = parameters => {                var manager = Context.GetAuthenticationManager();                if (manager == null) {                    throw new NotSupportedException("An OWIN authentication manager cannot be extracted from NancyContext");                }                var properties = new AuthenticationProperties {                    RedirectUri = "/"                };                // Instruct the OIDC client middleware to redirect the user agent to the identity provider.                // Note: the authenticationType parameter must match the value configured in Startup.cs                manager.Challenge(properties, OpenIdConnectAuthenticationDefaults.AuthenticationType);                return HttpStatusCode.Unauthorized;            };            Get["/signout"] = Post["/signout"] = parameters => {                var manager = Context.GetAuthenticationManager();                if (manager == null) {                    throw new NotSupportedException("An OWIN authentication manager cannot be extracted from NancyContext");                }                // Instruct the cookies middleware to delete the local cookie created when the user agent                // is redirected from the identity provider after a successful authorization flow.                manager.SignOut("ClientCookie");                // Instruct the OpenID Connect middleware to redirect                // the user agent to the identity provider to sign out.                manager.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationType);                return HttpStatusCode.OK;            };        }    }}

Code source: https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/blob/dev/samples/Nancy/Nancy.Client/Modules/AuthenticationModule.cs

Hope that helps!