public LoginActionResult(IViewService viewSvc, LoginViewModel model, SignInMessage message)
     : base(async () => await viewSvc.Login(model, message))
 {
     if (viewSvc == null) throw new ArgumentNullException("viewSvc");
     if (model == null) throw new ArgumentNullException("model");
     if (message == null) throw new ArgumentNullException("message");
 }
        private async Task<IHttpActionResult> RenderLoginPage(SignInMessage message, string signInMessageId, string errorMessage = null, string username = null, bool rememberMe = false)
        {
            if (message == null) throw new ArgumentNullException("message");

            username = username ?? lastUsernameCookie.GetValue();

            var idpRestrictions = await clientStore.GetIdentityProviderRestrictionsAsync(message.ClientId);
            var providers = context.GetExternalAuthenticationProviders(idpRestrictions);
            var providerLinks = context.GetLinksFromProviders(providers, signInMessageId);
            var visibleLinks = providerLinks.FilterHiddenLinks();

            if (errorMessage != null)
            {
                Logger.InfoFormat("rendering login page with error message: {0}", errorMessage);
            }
            else
            {
                if (options.AuthenticationOptions.EnableLocalLogin == false)
                {
                    Logger.Info("local login disabled");

                    string url = null;

                    if (providerLinks.Count() == 0)
                    {
                        Logger.Info("no providers registered for client");
                        return RenderErrorPage();
                    }
                    else if (providerLinks.Count() == 1)
                    {
                        Logger.Info("only one provider for client");
                        url = providerLinks.First().Href;
                    }
                    else if (visibleLinks.Count() == 1)
                    {
                        Logger.Info("only one visible provider");
                        url = visibleLinks.First().Href;
                    }

                    if (url.IsPresent())
                    {
                        Logger.InfoFormat("redirecting to provider URL: {0}", url);
                        return Redirect(url);
                    }
                }

                Logger.Info("rendering login page");
            }

            var loginPageLinks = options.AuthenticationOptions.LoginPageLinks.Render(Request.GetIdentityServerBaseUrl(), signInMessageId);

            var loginModel = new LoginViewModel
            {
                RequestId = context.GetRequestId(),
                SiteName = options.SiteName,
                SiteUrl = Request.GetIdentityServerBaseUrl(),
                CurrentUser = User.Identity.Name,
                ExternalProviders = visibleLinks,
                AdditionalLinks = loginPageLinks,
                ErrorMessage = errorMessage,
                LoginUrl = options.AuthenticationOptions.EnableLocalLogin ? Url.Route(Constants.RouteNames.Login, new { signin = signInMessageId }) : null,
                AllowRememberMe = options.AuthenticationOptions.CookieOptions.AllowRememberMe,
                RememberMe = options.AuthenticationOptions.CookieOptions.AllowRememberMe && rememberMe,
                LogoutUrl = Url.Route(Constants.RouteNames.Logout, null),
                AntiForgery = antiForgeryToken.GetAntiForgeryToken(),
                Username = username
            };

            return new LoginActionResult(viewService, loginModel, message);
        }
        private async Task<IHttpActionResult> RenderLoginPage(SignInMessage message, string signInMessageId, string errorMessage = null, string username = null, bool rememberMe = false)
        {
            if (message == null) throw new ArgumentNullException("message");

            username = username ?? GetLastUsernameFromCookie();

            var providers = await GetExternalProviders(message, signInMessageId);

            if (errorMessage != null)
            {
                Logger.InfoFormat("rendering login page with error message: {0}", errorMessage);
            }
            else
            {
                if (_authenticationOptions.EnableLocalLogin == false && providers.Count() == 1)
                {
                    // no local login and only one provider -- redirect to provider
                    Logger.Info("no local login and only one provider -- redirect to provider");
                    var url = Request.GetOwinEnvironment().GetIdentityServerHost();
                    url += providers.First().Href;
                    return Redirect(url);
                }
                else
                {
                    Logger.Info("rendering login page");
                }
            }

            var loginPageLinks = PrepareLoginPageLinks(signInMessageId, _authenticationOptions.LoginPageLinks);

            var loginModel = new LoginViewModel
            {
                SiteName = _options.SiteName,
                SiteUrl = Request.GetIdentityServerBaseUrl(),
                CurrentUser = await GetNameFromPrimaryAuthenticationType(),
                ExternalProviders = providers,
                AdditionalLinks = loginPageLinks,
                ErrorMessage = errorMessage,
                LoginUrl = _options.AuthenticationOptions.EnableLocalLogin ? Url.Route(Constants.RouteNames.Login, new { signin = signInMessageId }) : null,
                AllowRememberMe = _options.AuthenticationOptions.CookieOptions.AllowRememberMe,
                RememberMe = _options.AuthenticationOptions.CookieOptions.AllowRememberMe && rememberMe,
                LogoutUrl = Url.Route(Constants.RouteNames.Logout, null),
                AntiForgery = AntiForgeryTokenValidator.GetAntiForgeryHiddenInput(Request.GetOwinEnvironment()),
                Username = username
            };

            return new LoginActionResult(_viewService, loginModel, message);
        }
 /// <summary>
 /// Loads the HTML for the login page.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <param name="message">The message.</param>
 /// <returns>
 /// Stream for the HTML
 /// </returns>
 public virtual Task<Stream> Login(LoginViewModel model, SignInMessage message)
 {
     return Render(model, "login");
 }
Пример #5
0
 public virtual async Task<System.IO.Stream> Login(LoginViewModel model, SignInMessage message)
 {
     var client = await clientStore.FindClientByIdAsync(message.ClientId);
     var name = client != null ? client.ClientName : null;
     return await Render(model, "login", name);
 }