MultiProtocolIssuerSts.Controllers.AuthenticationController.Authenticate C# (CSharp) Method

Authenticate() public method

public Authenticate ( ) : System.Web.Mvc.ActionResult
return System.Web.Mvc.ActionResult
        public ActionResult Authenticate()
        {
            var identifier = new Uri(this.Request.QueryString[WSFederationConstants.Parameters.HomeRealm]);

            ClaimProvider issuer = this.configuration.RetrieveIssuer(identifier);
            if (issuer == null)
            {
                return this.HomeRealmDiscovery();
            }

            var handler = this.protocolDiscovery.RetrieveProtocolHandler(issuer);
            if (handler == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The protocol handler '{0}' was not found in the container", issuer.Protocol));
            }

            this.federationContext.IssuerName = issuer.Identifier.ToString();
            var scope = this.configuration.RetrieveScope(new Uri(this.federationContext.Realm));
            if (scope == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The scope '{0}' was not found in the configuration", this.federationContext.Realm));
            }

            handler.ProcessSignInRequest(scope, this.HttpContext);

            return new EmptyResult();
        }

Usage Example

        public void ShouldProcessSignInRequestWhenIdentityProviderIsProvided()
        {
            var protocolHandler = new Mock<IProtocolHandler>();
            var defaultProtocolDiscovery = new Mock<IProtocolDiscovery>();
            var federationContext = new Mock<IFederationContext>();
            var configuration = new Mock<IConfigurationRepository>();

            defaultProtocolDiscovery.Setup(s => s.RetrieveProtocolHandler(It.IsAny<ClaimProvider>()))
                .Returns(() => protocolHandler.Object);

            configuration.Setup(c => c.RetrieveIssuer(It.IsAny<Uri>()))
                .Returns(() => new ClaimProvider {
                                    Identifier = new Uri("https://identifier"),
                                    Url = new Uri("https://url")
                });
            configuration.Setup(c => c.RetrieveScope(It.IsAny<Uri>()))
                .Returns(() => new Scope
                {
                    Identifier = new Uri("https://relyingPartyIdentifier"),
                    Url = new Uri("https://url")
                });

            var controller = new AuthenticationController(defaultProtocolDiscovery.Object, federationContext.Object, configuration.Object);

            federationContext.SetupGet(s => s.Realm).Returns("https://relyingPartyIdentifier");
            federationContext.SetupGet(s => s.OriginalUrl).Returns("https://originalUrl");

            controller.SetFakeControllerContext();
            controller.Request.SetupRequestUrl("https://somedomain.com/?wa=wsignin1.0&wtrealm=blah&whr=https://identifier");

            controller.Authenticate();

            protocolHandler.Verify(
                p =>
                p.ProcessSignInRequest(
                    It.Is<Scope>(s => s.Identifier == new Uri("https://relyingPartyIdentifier")), It.IsAny<HttpContextBase>()),
                Times.Once());
        }