Exemple #1
0
        public override Task TicketReceived(TicketReceivedContext context)
        {
            if (!String.IsNullOrEmpty(_domainName))
            {
                var emailClaim = context.Ticket.Principal.Claims.FirstOrDefault(
                    c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress");

                if (emailClaim == null)
                {
                    context.Response.Redirect("/account/forbidden?reason=no_email_claim");
                    context.HandleResponse();
                }

                if (emailClaim.Value == null || !emailClaim.Value.ToLower().EndsWith(_domainName))
                {
                    context.Response.Redirect("/account/forbidden?reason=domain_not_allowed");
                    context.HandleResponse();
                }
            }

            return(base.TicketReceived(context));
        }
Exemple #2
0
        public async override Task TicketReceived(TicketReceivedContext context)
        {
            //如果不调用HandleResponse,默认的行为是重定向到RedirectUri属性指定的地址。
            //在这里不需要定向
            var handleResponseContext = new HandleResponseContext(context.HttpContext
                                                                  , context.Scheme
                                                                  , context.Options as WeChatClientOptions
                                                                  , context.Principal);

            await HandleResponse(handleResponseContext);

            context.HandleResponse();

            await base.TicketReceived(context);
        }
        private static async Task SetupIdentityAndClaimsAndGa(
            TicketReceivedContext context, OpenIdAuthOptions authOptions)
        {
            var id = await GetClaimsIdentity(
                context.Scheme.Name,
                context.Properties.GetTokenValue("access_token"),
                context.HttpContext,
                authOptions,
                context.Principal.Claims?.ToArray());

            await RegisterExternalAccount(context, id);

            try
            {
                await context.HttpContext.ConfirmAccountAsync(
                    id,
                    context.Properties.GetTokenValue("access_token"));
            }
            catch (Exception)
            {
                // Not logging here as it's already logged elsewhere.

                context.HandleResponse();
                context.Response.Redirect("/error/authfailure");

                return;
            }

            context.Principal = new ClaimsPrincipal(id);

            // The following is for GA.
            var subjectIdClaim = id.Claims.FirstOrDefault(x => x.Type == "sub");

            if (subjectIdClaim != null)
            {
                var distributedCache =
                    context.HttpContext.RequestServices.GetRequiredService <IDistributedCache>();

                await distributedCache.SetAsync($"JUST-LOGGED-IN-{subjectIdClaim?.Value}",
                                                Encoding.UTF8.GetBytes("TRUE"),
                                                new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5)
                });
            }
        }