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

时间:2018-02-13 07:10:07

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

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

enter image description here

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登录并验证用户身份。非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

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

$arr_child_par

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

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