ASP.NET OWIN自定义Cookie身份验证

Jas*_*han 5 asp.net authentication cookies asp.net-mvc owin

我们正在运行一个经典的asp web应用程序,并希望它与新开发的MVC应用程序一起工作.我们希望在MVC应用程序中使用经典asp应用程序的身份验证.

这个想法是当用户登录经典的asp应用程序时,它将发出一种auth cookie,cookie是用我们自己的方法加密的.Cookie将包含使用标识.

客户端然后浏览到MVC应用程序以及此身份验证cookie.MVC应用程序将检查cookie是否存在并验证它.它不会重定向到经典的asp登录页面.

所以我想自定义OWIN cookie身份验证以使用我自己的身份验证逻辑.我试图实现CookieAuthenicationProvider,但我不知道在哪里放我的逻辑.

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            CookieName = ".classicauth",
            CookieSecure = CookieSecureOption.SameAsRequest,
            CookieHttpOnly = true,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = context => {
                    //?? where I can extract the cookie and validate it??
                    context.RejectIdentity();
                    return Task.FromResult<int>(0);
                },
                OnApplyRedirect = context => {
                    context.Response.Redirect("classic_asp_login_url");
                }
            }
        });            
Run Code Online (Sandbox Code Playgroud)

CookieAuthenticationProvider具有OnValidateIdentity,但它似乎不是提取cookie并验证它的正确位置.

谢谢.杰森.

小智 2

我还没有在特定的环境下亲自测试过。但 CookieManager 对我有用。

OnValidateIdentity = context => {
  var cookie = context.Options.CookieManager.GetRequestCookie(context.OwinContext, context.Options.CookieName);
  context.RejectIdentity();
  return Task.FromResult<int>(0);
},
Run Code Online (Sandbox Code Playgroud)