using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.DirectoryServices;
using System.Security.Principal;
using Microsoft.SharePoint;
using System.Configuration;
using YAF.Providers;
using YAF.Classes.Core;
using YAF.Classes.Utils;
using YAF.Classes.Data;
public class User
{
public string UserID { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public string HomePage { get; set; }
public string Location { get; set; }
}
public partial class NewUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if ((Request.Form["UserID"] != null))
{
string _appName = "YetAnotherForum";
string _username = !string.IsNullOrEmpty(Request.Form["UserID"].ToString()) ? Request.Form["UserID"].ToString() : string.Empty;
string _displayName = !string.IsNullOrEmpty(Request.Form["DisplayName"].ToString()) ? Request.Form["DisplayName"].ToString() : string.Empty;
string _password = "Password2";
string _passwordSalt = "zorgBpCN0TLvnRY1VSmwDxCEiE0=";
int _passwordFormat = 1;
string _email = !string.IsNullOrEmpty(Request.Form["Email"].ToString()) ? Request.Form["Email"].ToString() : string.Empty;
bool _isApproved = true;
int _boardid = 1;
YafContext.Current.Cache.Clear() ;
//Check to see if user exists in Provider table.
Object userKey = YAF.Providers.Profile.DB.Current.GetProviderUserKey(_appName, _username ) ;
//Add user to yaf_prov_Membership table
YAF.Providers.Membership.DB.Current.CreateUser(_appName, _username, _password, "", _passwordFormat, _email, "", "", _isApproved, userKey ) ;
YAF.Providers.Roles.DB.Current.AddUserToRole(_appName, _username, "Registered" ) ;
// create the user in the YAF DB so profile can ge created...
System.Web.Security.MembershipUser user = UserMembershipHelper.GetUser(_username ) ;
int? userId = RoleMembershipHelper.CreateForumUser(user,_displayName,_boardid ) ;
Response.Redirect("~" ) ;
}
}
public void BuildUserInfo()
{
User myUser = GetUserData();
if (!myUser.UserID.Equals(@"NTAuthority\Anonymous" ) )
{
Response.Write(@"
" ) ; Response.Write("
" ) ; Response.Write(string.Format(@"
UserID : | | ", myUser.UserID, "UserID" ) + Environment.NewLine ) ; Response.Write("
" ) ; Response.Write("
" ) ; Response.Write(string.Format(@"
Display Name : | | ", myUser.DisplayName, "DisplayName" ) + Environment.NewLine ) ; Response.Write("
" ) ; Response.Write("
" ) ; Response.Write(string.Format(@"
Email : | | ", myUser.Email, "Email" ) + Environment.NewLine ) ; Response.Write("
" ) ; Response.Write("
" ) ; Response.Write(string.Format(@"
Home Page: | | ", myUser.HomePage, "HomePage" ) + Environment.NewLine ) ; Response.Write("
" ) ; Response.Write("
" ) ;
}
else
{
Response.Write("This site requires your browser to be configured with Windows Integrated Authentication.
" ) ;
Response.Write("Please follow the instructions using the following link: " ) ;
Response.Write(@"
" ) ; //This section is optional, but the browser settings were not fully managed by Group Policy in our environment thus needing this.
}
}
public User GetUserData()
{
User currentUser = new User();
currentUser.UserID = HttpContext.Current.User.Identity.Name.ToString() ;
if (!currentUser.UserID.Equals(@"NTAuthority\Anonymous" ) )
{
int subIndex = currentUser.UserID.IndexOf("\\" ) ;
string sAMAccountName = currentUser.UserID.Substring(subIndex + 1 ) ;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string appid = WindowsIdentity.GetCurrent().Name;
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://CONTOSO.MICROSOFT.COM" ) ; //BE SURE TO UPDATE THIS WITH YOUR DOMAIN NAME
DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry ) ;
SearchResultCollection src = null;
//THIS QUERY CHECKS TO MAKE SURE THE ACCOUNT IS NOT DISABLED.
dirSearcher.Filter = string.Format("(&(anr={0})(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)) ) " , sAMAccountName);
dirSearcher.PageSize = 50;
src = dirSearcher.FindAll() ;
if (src != null)
{
SearchResult sr = src[0];
DirectoryEntry dirUser = sr.GetDirectoryEntry() ;
string PreferredName = ((dirUser.Properties["cn"].Value != null) ? dirUser.Properties["cn"].Value.ToString() : "" ) ;
string firstName = ((dirUser.Properties["givenName"].Value != null) ? dirUser.Properties["givenName"].Value.ToString() : "" ) ;
string lastName = ((dirUser.Properties["sn"].Value != null) ? dirUser.Properties["sn"].Value.ToString() : "" ) ;
if (firstName != "" && lastName != "" )
{
currentUser.DisplayName = lastName + ", " + firstName;
}
else
{
currentUser.DisplayName = PreferredName;
}
currentUser.Email = (dirUser.Properties["mail"].Value != null) ? dirUser.Properties["mail"].Value.ToString() : "";
currentUser.HomePage = (dirUser.Properties["wwwHomePage"].Value != null) ? dirUser.Properties["wwwHomePage"].Value.ToString() : "";
currentUser.Location = (dirUser.Properties["physicalDeliveryOfficeName"].Value != null) ? dirUser.Properties["physicalDeliveryOfficeName"].Value.ToString() : "";
}
} ) ;
}
return currentUser;
}
}