ASP.NET Identity 2.0 Decrypts Owin Cookie

I work in a server application where I apply multi-level rent. On this side of the server, I have Backoffice ( ASP.NET MVC ) and BackEnd ( WCF ).

I want to decrypt the Identity cookie in order to test its functionality and use it for authorization in WCF services.

To be more specific, I really want to know if the ASP.NET Identity API provides any service, for example, the following example (it would work if I used Authentication forms)

FormsAuthenticationTicket formsTicket = FormsAuthentication.Decrypt(tokenValue);

Thanks in advance.

+4
source share
2 answers

. :

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

, , , cookie, :

HttpContext.Current.User

, ,

+9

ASP.NET cookie, TicketDataFormat IDataProtector ( Startup.Auth.cs):

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    TicketDataFormat = new TicketDataFormat(...), // Use your own TicketDataFormat
    Provider = new CookieAuthenticationProvider
    {       
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});

TicketDataFormat , AuthenticationTicket :

AuthenticationTicket ticket = options.TicketDataFormat.Unprotect(cookie.Value);
+2

All Articles