webdav portal auth via ad

This commit is contained in:
vfedosevich 2015-01-13 04:18:56 -08:00
parent 05d9fddb5d
commit 7dd090820b
56 changed files with 927 additions and 281 deletions

View file

@ -0,0 +1,74 @@
using System;
using System.DirectoryServices.AccountManagement;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using WebsitePanel.WebDav.Core.Interfaces.Security;
using WebsitePanel.WebDav.Core.Security.Authentication.Principals;
using WebsitePanel.WebDav.Core.Security.Cryptography;
using WebsitePanel.WebDav.Core.Wsp.Framework;
namespace WebsitePanel.WebDav.Core.Security.Authentication
{
public class FormsAuthenticationService : IAuthenticationService
{
private readonly ICryptography _cryptography;
private readonly PrincipalContext _principalContext;
public FormsAuthenticationService(ICryptography cryptography)
{
_cryptography = cryptography;
_principalContext = new PrincipalContext(ContextType.Domain);
}
public WspPrincipal LogIn(string login, string password)
{
if (_principalContext.ValidateCredentials(login, password) == false)
{
return null;
}
var principal = new WspPrincipal(login);
var exchangeAccount = WSP.Services.ExchangeServer.GetAccountByAccountNameWithoutItemId(login);
var organization = WSP.Services.Organizations.GetOrganization(exchangeAccount.ItemId);
principal.AccountId = exchangeAccount.AccountId;
principal.ItemId = exchangeAccount.ItemId;
principal.OrganizationId = organization.OrganizationId;
principal.DisplayName = exchangeAccount.DisplayName;
principal.EncryptedPassword = _cryptography.Encrypt(password);
CreateAuthenticationTicket(principal);
HttpContext.Current.User = principal;
return principal;
}
public void CreateAuthenticationTicket(WspPrincipal principal)
{
var serializer = new JavaScriptSerializer();
string userData = serializer.Serialize(principal);
var authTicket = new FormsAuthenticationTicket(1, principal.Identity.Name, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout),
FormsAuthentication.SlidingExpiration, userData);
var encTicket = FormsAuthentication.Encrypt(authTicket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
if (FormsAuthentication.SlidingExpiration)
{
cookie.Expires = authTicket.Expiration;
}
HttpContext.Current.Response.Cookies.Add(cookie);
}
public void LogOut()
{
FormsAuthentication.SignOut();
}
}
}

View file

@ -0,0 +1,47 @@
using System.Security.Principal;
using System.Web.Script.Serialization;
using System.Web.Security;
using System.Xml.Serialization;
namespace WebsitePanel.WebDav.Core.Security.Authentication.Principals
{
public class WspPrincipal : IPrincipal
{
public int AccountId { get; set; }
public string OrganizationId { get; set; }
public int ItemId { get; set; }
public string Login { get; set; }
public string EncryptedPassword { get; set; }
public string DisplayName { get; set; }
public string UserName
{
get
{
return !string.IsNullOrEmpty(Login) ? Login.Split('@')[0] : string.Empty;
}
}
[XmlIgnore, ScriptIgnore]
public IIdentity Identity { get; private set; }
public WspPrincipal(string username)
{
Identity = new GenericIdentity(username);
Login = username;
}
public WspPrincipal()
{
}
public bool IsInRole(string role)
{
return Identity.IsAuthenticated
&& !string.IsNullOrWhiteSpace(role)
&& Roles.IsUserInRole(Identity.Name, role);
}
}
}