c# - 使用 owin 和 Nancy 进行 Oauth 身份验证

标签 c# asp.net owin nancy katana

按照本指南在 Owin 上使用 MVC 5 进行外部身份验证 - External login providers with owinkatana .

我已将以下内容添加到我的 Owin 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(南希模块)

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 代码?

最佳答案

我将对我即将发表的评论进行扩展,并将其作为答案(即使您似乎离开了 Nancy)。我 asked a similar question , 并指向 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

希望对您有所帮助!

关于c# - 使用 owin 和 Nancy 进行 Oauth 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786973/

相关文章:

c# - 从 .NET 代码中提取数据

javascript - Asp .NET HiddenField 导致无效的回发或回调参数

c# - 使用 Owin + OAuth + Google 在 ExternalLogin 上从 HTTP 重定向到 HTTPS

asp.net-web-api - 在 WebAPI 中的查询字符串中传递并验证 OWIN Bearer token

c# - VB.NET linq group by 匿名类型不能按预期工作

c# - LINQ、输出参数和 'Use of Unassigned Local Variable' 错误

c# - 系统.Enum.GetValues : In C# not the same as in VB?

asp.net - 当我单击其中一个更新面板中的按钮时,如何防止所有更新面板重新加载?

asp.net - 图像在 asp.net 中不显示

c# - OWIN 初创公司剖析