I have made a few changes to the AuthenticationService so that it can be used from any portal and child portals as well. The previous post had a few hard coded variables to get the ball rolling.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Ria;
using System.Web.Ria.ApplicationServices;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security.Membership;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Authentication;
using System.Web;
namespace AuthenticationServices
{
[EnableClientAccess]
public class AuthenticationService : AuthenticationBase<User>
{
private bool _loggedIn;
private User _user;
private readonly HttpRequest _request;
private readonly string _domainName;
private readonly PortalAliasInfo _portalAlias;
private readonly PortalSettings _portalSettings;
public AuthenticationService()
{
_request = HttpContext.Current.Request;
_domainName = _request.Url.ToString().Substring(7, _request.Url.ToString().IndexOf("/ClientBin") - 7);
_portalAlias = PortalAliasController.GetPortalAliasInfo(_domainName);
_portalSettings = new PortalSettings(Null.NullInteger, _portalAlias.PortalID);
}
protected override User GetAuthenticatedUser(System.Security.Principal.IPrincipal principal)
{
if (_request.IsAuthenticated || _loggedIn)
{
var userInfo = UserController.GetUserByName(0, principal.Identity.Name, false);
var roleController = new RoleController();
var portalRoles = roleController.GetRolesByUser(userInfo.UserID, _portalSettings.PortalId);
_user = new User()
{
AffiliateID = userInfo.AffiliateID,
DisplayName = userInfo.DisplayName,
Email = userInfo.Email,
FirstName = userInfo.FirstName,
IsDeleted = userInfo.IsDeleted,
Name = userInfo.DisplayName,
Roles = portalRoles
};
}
return _user;
}
protected override bool ValidateUser(string userName, string password)
{
var loginStatus = UserLoginStatus.LOGIN_FAILURE;
UserController.ValidateUser(_portalSettings.PortalId, userName, password, "DNN", "",
_portalSettings.PortalName, "127.0.0.1", ref loginStatus);
switch (loginStatus)
{
case UserLoginStatus.LOGIN_SUCCESS:
case UserLoginStatus.LOGIN_SUPERUSER:
_loggedIn = true;
return true;
break;
default:
return false;
break;
}
}
}
public class User : UserBase
{
public int AffiliateID { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public bool IsDeleted { get; set; }
}
}