示例#1
0
        public void ProcessRequest(HttpContext context)
        {
            if (context.User is ClaimsPrincipal principal)
            {
                if (principal.Identity?.IsAuthenticated == false)
                {
                    context.Response.Write("Not Authenticated");
                    context.Response.StatusCode = 401;
                    context.Response.Flush();
                    context.Response.End();
                    return;
                }

                var messagePrincipal = MessagePrincipal.FromClaimsPrincipal(principal);
                var bytes            = MessagePackSerializer.Serialize(messagePrincipal);
                context.Response.BinaryWrite(bytes);
                context.Response.StatusCode = 200;
                context.Response.Flush();
                context.Response.End();
                return;
            }

            context.Response.StatusCode = 401;
            context.Response.Flush();
            context.Response.End();
        }
示例#2
0
        public void ProcessRequest(HttpContextBase context)
        {
            context.Response.TrySkipIisCustomErrors = true;
            context.Response.SuppressFormsAuthenticationRedirect = true;

            if (context.User is ClaimsPrincipal principal)
            {
                if (principal.Identity?.IsAuthenticated == false)
                {
                    context.Response.Write("Not Authenticated");
                    context.Response.StatusCode = 401;
                    context.Response.Flush();
                    context.Response.End();
                    return;
                }

                var messagePrincipal = MessagePrincipal.FromClaimsPrincipal(principal);
                var bytes            = MessagePackSerializer.Serialize(messagePrincipal);
                context.Response.BinaryWrite(bytes);
                context.Response.StatusCode = 200;
                context.Response.Flush();
                context.Response.End();
                return;
            }

            context.Response.StatusCode = 401;
            context.Response.Flush();
            context.Response.End();
        }
示例#3
0
        public static MessagePrincipal FromClaimsPrincipal(ClaimsPrincipal source)
        {
            var messagePrincipal = new MessagePrincipal
            {
                Identities = new List <MessageIdentity>(),
            };

            foreach (var sourceIdentity in source.Identities)
            {
                var messageIdentity = new MessageIdentity
                {
                    AuthenticationType = sourceIdentity.AuthenticationType,
                    IsAuthenticated    = sourceIdentity.IsAuthenticated,
                    Label         = sourceIdentity.Label,
                    NameClaimType = sourceIdentity.NameClaimType,
                    RoleClaimType = sourceIdentity.RoleClaimType,
                    Claims        = new List <MessageClaim>(),
                };

                foreach (var sourceClaim in sourceIdentity.Claims)
                {
                    var messageClaim = new MessageClaim
                    {
                        Issuer         = sourceClaim.Issuer,
                        OriginalIssuer = sourceClaim.OriginalIssuer,
                        Properties     = sourceClaim.Properties,
                        Type           = sourceClaim.Type,
                        Value          = sourceClaim.Value,
                        ValueType      = sourceClaim.ValueType
                    };
                    messageIdentity.Claims.Add(messageClaim);
                }

                messagePrincipal.Identities.Add(messageIdentity);
            }

            return(messagePrincipal);
        }