ASP.NET Forms 应用程序中的 OWIN 上下文未正确初始化

OWIN Context is not initialized properly in ASP.NET Forms application

我是 OWIN 和 ADFS 的新手。我正在尝试使用 OWIN 中间件对来自 ADFS 的用户进行身份验证。但是当我 运行 应用程序并执行登录时, return 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 登录并对用户进行身份验证。非常感谢任何帮助。谢谢。

发现问题,我错过了中间件中的 运行 方法 - 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);
            }
        });
    }

但是如果我们只想对某些特定路径执行extensions/middle-wares,那么我们可以使用app.Use(),这只是它的一种用法。

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