websitepanel/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/AccountController.cs
2014-12-30 01:49:58 -08:00

91 lines
No EOL
4.5 KiB
C#

using System;
using System.Configuration;
using System.DirectoryServices;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Microsoft.Win32;
using Ninject;
using WebsitePanel.EnterpriseServer;
using WebsitePanel.Portal;
using WebsitePanel.Providers.HostedSolution;
using WebsitePanel.WebDavPortal.Config;
using WebsitePanel.WebDavPortal.Cryptography;
using WebsitePanel.WebDavPortal.DependencyInjection;
using WebsitePanel.WebDavPortal.Exceptions;
using WebsitePanel.WebDavPortal.Models;
using System.Collections.Generic;
using WebsitePanel.Providers.OS;
using WebDAV;
namespace WebsitePanel.WebDavPortal.Controllers
{
public class AccountController : Controller
{
private readonly IKernel _kernel = new StandardKernel(new NinjectSettings {AllowNullInjection = true}, new WebDavExplorerAppModule());
[HttpGet]
public ActionResult Login()
{
object isAuthentication = _kernel.Get<AccountModel>();
if (isAuthentication != null)
return RedirectToAction("ShowContent", "FileSystem");
return View();
}
[HttpPost]
public ActionResult Login(AccountModel model)
{
//var ldapConnectionString = WebDavAppConfigManager.Instance.ConnectionStrings.LdapServer;
//if (ldapConnectionString == null || !Regex.IsMatch(ldapConnectionString, @"^LDAP://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$"))
// return View(new AccountModel { LdapError = "LDAP server address is invalid" });
//var principal = new WebDavPortalIdentity(model.Login, model.Password);
//bool isAuthenticated = principal.Identity.IsAuthenticated;
//var organizationId = principal.GetOrganizationId();
AutheticationToServicesUsingWebsitePanelUser();
var exchangeAccount = ES.Services.ExchangeServer.GetAccountByAccountNameWithoutItemId(model.Login);
var isAuthenticated = exchangeAccount != null && exchangeAccount.AccountPassword == model.Password;
ViewBag.LdapIsAuthentication = isAuthenticated;
if (isAuthenticated)
{
Session[WebDavAppConfigManager.Instance.SessionKeys.ItemId] = exchangeAccount.ItemId;
try
{
Session[WebDavAppConfigManager.Instance.SessionKeys.AccountInfo] = model;
Session[WebDavAppConfigManager.Instance.SessionKeys.WebDavManager] = new WebDavManager(new NetworkCredential(model.Login, model.Password, WebDavAppConfigManager.Instance.UserDomain), exchangeAccount.ItemId);
//Session[WebDavAppConfigManager.Instance.SessionKeys.WebDavManager] = new WebDavManager(new NetworkCredential("Administrator", "WSP99cc$$1", WebDavAppConfigManager.Instance.UserDomain), exchangeAccount.ItemId);
}
catch (ConnectToWebDavServerException exception)
{
return View(new AccountModel { LdapError = exception.Message });
}
return RedirectToAction("ShowContent", "FileSystem", new { org = _kernel.Get<IWebDavManager>().OrganizationName });
}
return View(new AccountModel { LdapError = "The user name or password is incorrect" });
}
private void AutheticationToServicesUsingWebsitePanelUser()
{
var crypto = _kernel.Get<ICryptography>();
var websitePanelLogin = crypto.Decrypt(WebDavAppConfigManager.Instance.WebsitePanelConstantUserParameters.Login);
var websitePanelPassword = crypto.Decrypt(WebDavAppConfigManager.Instance.WebsitePanelConstantUserParameters.Password);
var authTicket = new FormsAuthenticationTicket(1, websitePanelLogin, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout),
FormsAuthentication.SlidingExpiration, websitePanelPassword + Environment.NewLine);
var encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
if (FormsAuthentication.SlidingExpiration)
authCookie.Expires = authTicket.Expiration;
Response.Cookies.Add(authCookie);
}
}
}