ASP.NET Identity 2.0解密Owin cookie

Dio*_*nha 5 asp.net cookies asp.net-mvc wcf asp.net-identity

我正在申请多租户的服务器端应用程序.在这个服务器端,我有一个Backoffice(ASP.NET MVC)和一个BackEnd(WCF).

我想解密Identity cookie,以便我可以检查它是否有效并使用它在WCF服务中进行身份验证.

更具体一点,我真的想知道ASP.NET Identity API是否提供了类似以下示例的任何类型的服务(如果我使用表单身份验证,它将起作用)

FormsAuthenticationTicket formsTicket = FormsAuthentication.Decrypt(tokenValue);
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Dio*_*nha 14

经过大量的研究,我在博客中找到了一种方法.最终算法如下所示:

      private bool BackOfficeUserAuthorized(string ticket)
      {
        ticket = ticket.Replace('-', '+').Replace('_', '/');

        var padding = 3 - ((ticket.Length + 3) % 4);
        if (padding != 0)
            ticket = ticket + new string('=', padding);

        var bytes = Convert.FromBase64String(ticket);

        bytes = System.Web.Security.MachineKey.Unprotect(bytes,
            "Microsoft.Owin.Security.Cookies.CookieAuthenticationMiddleware",
                "ApplicationCookie", "v1");

        using (var memory = new MemoryStream(bytes))
        {
            using (var compression = new GZipStream(memory,
                                                CompressionMode.Decompress))
            {
                using (var reader = new BinaryReader(compression))
                {
                    reader.ReadInt32();
                    string authenticationType = reader.ReadString();
                    reader.ReadString();
                    reader.ReadString();

                    int count = reader.ReadInt32();

                    var claims = new Claim[count];
                    for (int index = 0; index != count; ++index)
                    {
                        string type = reader.ReadString();
                        type = type == "\0" ? ClaimTypes.Name : type;

                        string value = reader.ReadString();

                        string valueType = reader.ReadString();
                        valueType = valueType == "\0" ?
                                       "http://www.w3.org/2001/XMLSchema#string" :
                                         valueType;

                        string issuer = reader.ReadString();
                        issuer = issuer == "\0" ? "LOCAL AUTHORITY" : issuer;

                        string originalIssuer = reader.ReadString();
                        originalIssuer = originalIssuer == "\0" ?
                                                     issuer : originalIssuer;

                        claims[index] = new Claim(type, value,
                                               valueType, issuer, originalIssuer);
                    }

                    var identity = new ClaimsIdentity(claims, authenticationType,
                                                  ClaimTypes.Name, ClaimTypes.Role);

                    var principal = new ClaimsPrincipal(identity);

                    return principal.Identity.IsAuthenticated;
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

请注意,委托人就像发送您刚刚调用的身份验证cookie一样:

HttpContext.Current.User
Run Code Online (Sandbox Code Playgroud)

如果您对了解算法的工作原理感兴趣,可以在此处找到