【问题标题】:OWIN Context is not initialized properly in ASP.NET Forms applicationOWIN 上下文未在 ASP.NET Forms 应用程序中正确初始化
【发布时间】:2018-07-23 11:55:36
【问题描述】:

我是 OWIN 和 ADFS 的新手。我正在尝试使用 OWIN 中间件对来自 ADFS 的用户进行身份验证。但是当我运行应用程序并执行登录时,返回 HttpContext.Current.GetOwinContext() 没有正确初始化。

owin_middleware_startup.cs

public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
        ConfigureAuth(app);

    }

    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(
        new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, // application cookie which is generic for all the authentication types.
            LoginPath = new PathString("/login.aspx"), // redirect if not authenticated.
            AuthenticationMode = AuthenticationMode.Passive
        });

        app.UseWsFederationAuthentication(
        new WsFederationAuthenticationOptions
        {
            MetadataAddress = "https://adfs-server/federationmetadata/2007-06/federationmetadata.xml", //adfs meta data.
            Wtrealm = "https://localhost/", //reltying party
            Wreply = "/home.aspx" // redirect
        });

        app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ApplicationCookie);
    }

login.aspx.cs

    private IAuthenticationManager AuthenticationManager
    {
        get { return HttpContext.Current.GetOwinContext().Authentication; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void loginSSObtn_Click(object sender, EventArgs e)
    {
        IdentitySignin("administrator");
    }

    private void IdentitySignin(string userName)
    {
        //Create list of claims for Identity
        var claims = new List<Claim>();
        claims.Add(new Claim(ClaimTypes.Name, userName));

        var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

        AuthenticationManager.SignIn(new AuthenticationProperties()
        {
            AllowRefresh = true,
            IsPersistent = true,
            IssuedUtc = DateTime.UtcNow,
            ExpiresUtc = DateTime.UtcNow.AddDays(2)
        }, identity);

        //Response.Redirect("/home.aspx");
    }

我的目标是重定向到 ADFS 登录并对用户进行身份验证。非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: c# asp.net adfs2.0 owin-middleware


    【解决方案1】:

    发现问题,我错过了中间件中的 RUN 方法 - app.Run()。这会将扩展名插入到 OWIN 启动中。并为所有请求执行它。 修复:

    public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
            ConfigureAuth(app);
    
        }
    
        public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ApplicationCookie);
            app.UseCookieAuthentication(
            new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, // application cookie which is generic for all the authentication types.
                LoginPath = new PathString("/login.aspx"), // redirect if not authenticated.
                AuthenticationMode = AuthenticationMode.Passive
            });
    
            app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                AuthenticationType = "test auth",
                MetadataAddress = "https://adfs-server/federationmetadata/2007-06/federationmetadata.xml", //adfs meta data.
                Wtrealm = "https://localhost/", //reltying party
                Wreply = "/home.aspx"//redirect
            });
    
            AuthenticateAllRequests(app, "test auth");
    
        }
    
        private static void AuthenticateAllRequests(IAppBuilder app, params string[] authenticationTypes)
        {
            app.Use((context, continuation) =>
            {
                if (context.Authentication.User != null &&
                    context.Authentication.User.Identity != null &&
                    context.Authentication.User.Identity.IsAuthenticated)
                {
                    return continuation();
                }
                else
                {
                    context.Authentication.Challenge(authenticationTypes);
                    return Task.Delay(0);
                }
            });
        }
    

    但是如果我们只想为某些特定路径执行扩展/中间件,那么我们可以使用app.Use(),这只是它的一种用法。

    如果我错了,请随时纠正我。

    【讨论】:

    • 在我的混合身份验证应用程序中,似乎在某些随机请求应用程序中没有获得身份。在找到您的答案之前,我无法处理该问题。 AuthenticateAllRequests 方法救了我的命。非常感谢!!!
    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 2010-11-07
    相关资源
    最近更新 更多