Oauth认证与自己& &;南希

本文关键字:南希 自己 认证 Oauth | 更新日期: 2023-09-27 18:04:22

遵循在Owin上使用MVC 5进行外部认证的指南-使用owinkatana进行外部登录提供程序。

我在自己的Nancy应用程序

中添加了以下内容

Startup.cs——

app.Properties["Microsoft.Owin.Security.Constants.DefaultSignInAsAuthenticationType"] = "ExternalCookie";
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = "ExternalCookie",
    AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive,
});
app.UseTwitterAuthentication(new TwitterAuthenticationOptions
{
    ConsumerKey = "mykey",
    ConsumerSecret = "mypass"
});

LoginModule.cs (nancy module)

Post["ExternalLogin"] = _ =>
{
    var provider = Request.Form.name;
    var auth = Context.GetAuthenticationManager();
    auth.Challenge(new AuthenticationProperties
    {
        RedirectUri = String.Format("/?provder={0}", provider)
    }, provider);
    return HttpStatusCode.Unauthorized;
};

现在在挑战点什么都没有发生。它只显示一个带有重定向Url的空白页面。我已经确认,我可以让它在MVC下面的例子工作。有人知道这部分的正确Nancy代码吗?

Oauth认证与自己& &;南希

我将对我即将留下的评论进行扩展,并将其作为一个答案(尽管您似乎离开了Nancy)。我问了一个类似的问题,并指向github上的以下代码示例:

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

假设您已经在Startup.cs中正确地连接了OIDC,下面的代码是我需要让Nancy模块触发我的登录/签出路由上的身份验证的代码:

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;
            };
        }
    }
}

代码来源:https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/blob/dev/samples/Nancy/Nancy.Client/Modules/AuthenticationModule.cs

希望有帮助!