Beispiel #1
0
        public async Task <bool> InvokeReturnPathAsync()
        {
            AuthenticationTicket?    ticket     = null;
            Exception?               exception  = null;
            AuthenticationProperties?properties = null;

            try
            {
                ticket = await AuthenticateAsync().ConfigureAwait(false);

                if (ticket?.Identity == null || !ticket.Identity.IsAuthenticated)
                {
                    exception  = new InvalidOperationException("Invalid return state, unable to redirect.");
                    properties = ticket?.Properties;
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            if (exception != null)
            {
                _logger.WriteWarning(exception.Message);

                var errorContext = new CasRemoteFailureContext(Context, exception)
                {
                    Properties = properties
                };

                await Options.Provider.RemoteFailure(errorContext).ConfigureAwait(false);

                if (errorContext.Handled)
                {
                    return(true);
                }

                if (errorContext.Skipped)
                {
                    return(false);
                }

                Response.StatusCode = 500;

                if (errorContext.Failure != null)
                {
                    throw new Exception("An error was encountered while handling the remote login.", errorContext.Failure);
                }
            }

#pragma warning disable CS8604 // Possible null reference argument.
            var context = new CasRedirectToAuthorizationEndpointContext(Context, ticket)
#pragma warning restore CS8604 // Possible null reference argument.
            {
                SignInAsAuthenticationType = Options.SignInAsAuthenticationType,
                RedirectUri = ticket?.Properties.RedirectUri
            };

            if (ticket != null)
            {
                ticket.Properties.RedirectUri = null;
            }

            await Options.Provider.RedirectToAuthorizationEndpoint(context).ConfigureAwait(false);

            if (context.SignInAsAuthenticationType != null && context.Identity != null)
            {
                var signInIdentity = context.Identity;
                if (!string.Equals(signInIdentity.AuthenticationType, context.SignInAsAuthenticationType, StringComparison.Ordinal))
                {
                    signInIdentity = new ClaimsIdentity(signInIdentity.Claims, context.SignInAsAuthenticationType, signInIdentity.NameClaimType, signInIdentity.RoleClaimType);
                }
                Context.Authentication.SignIn(context.Properties, signInIdentity);
            }

            if (!context.IsRequestCompleted && context.RedirectUri != null)
            {
                if (context.Identity == null)
                {
                    // add a redirect hint that sign-in failed in some way
                    context.RedirectUri = WebUtilities.AddQueryString(context.RedirectUri, "error", "access_denied");
                }
                Response.Redirect(context.RedirectUri);
                context.RequestCompleted();
            }

            return(context.IsRequestCompleted);
        }
Beispiel #2
0
 public Task RemoteFailure(CasRemoteFailureContext context) => OnRemoteFailure(context);