简体   繁体   English

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

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

I'm new to OWIN and ADFS. 我是OWIN和ADFS的新手。 I'm trying to authenticate users from ADFS using OWIN middleware. 我正在尝试使用OWIN中间件从ADFS验证用户。 But when i run the app and perform login, the return HttpContext.Current.GetOwinContext() is not initialized properly. 但是,当我运行该应用程序并执行登录时,返回的HttpContext.Current.GetOwinContext()未正确初始化。

在此处输入图片说明

owin_middleware_startup.cs 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 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");
    }

My goal is to redirect to the ADFS login and authenticate the user. 我的目标是重定向到ADFS登录并验证用户。 Highly appreciate any help. 非常感谢您的帮助。 Thanks. 谢谢。

Found the issue, I had missed the RUN method - app.Run() in the middle-ware. 发现问题后,我错过了RUN方法-中间件中的app.Run() This inserts the extension to the OWIN startup. 这会将扩展插入到OWIN启动中。 And executes it for all the requests. 并针对所有请求执行它。 Fix : 解决:

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

But if we want to execute the extensions/middle-wares only for some specific path then we can use app.Use() this is just one usage of it. 但是,如果我们只想为某些特定路径执行扩展/中间件,则可以使用app.Use(),这只是它的一种用法。

feel free to correct me if i'm wrong. 如果我错了,请随时纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM