Registration Form
If anybody wants to register only user of specific intranet then they will check if user is exist on intranet domain or not. If available then add record to UserInfo.Xml file of app_data folder.
If user is already exists then show message for that.
Users is a dataset contain username and useremail.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.DirectoryServices.AccountManagement;
namespace MobileEnabledWebFormsApp.Mobile
{
public partial class RegistrationForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
loginlink.Visible = false;
}
protected void btnAddUser_Click(object sender, EventArgs e)
{
//Add new Record to XML File
string domain = "Domain Name";
string uname = txtUserName.Text.Trim();
if (UserExists(uname, domain))
{
Users users = new Users();
users.ReadXml(Server.MapPath("~/App_Data/UsersInfo.xml"));
//Check if user allready exists or not
if (users != null && users.Tables.Count > 0 && users.Tables[0].Rows.Count > 0)
{
int i;
bool flag = false;
for (i = 0; i < users.Tables[0].Rows.Count; i++)
{
if (users.Tables[0].Rows[i].ItemArray[0].ToString() == uname)
{
flag = true;
}
}
if (flag == false)
{
//If user exists in domain but not in Xml then add user
Users.UserRow ur = users.User.NewUserRow();
ur.UserName = txtUserName.Text.Trim();
ur.UserEmail = txtUserEmail.Text.Trim();
users.User.AddUserRow(ur);
users.WriteXml(Server.MapPath("~/App_Data/UsersInfo.xml"));
lblError.ForeColor = Color.Green;
lblError.Text = "User Registered Successfully!";
loginlink.Visible = true;
}
else
{
lblError.ForeColor = Color.Green;
lblError.Text = "User Allready Exists!";
loginlink.Visible = true;
}
}
else
{
Users.UserRow ur = users.User.NewUserRow();
ur.UserName = txtUserName.Text.Trim();
ur.UserEmail = txtUserEmail.Text.Trim();
users.User.AddUserRow(ur);
users.WriteXml(Server.MapPath("~/App_Data/UsersInfo.xml"));
lblError.ForeColor = Color.Green;
lblError.Text = "User Registered Successfully!";
loginlink.Visible = true;
}
txtUserId.Text = "";
txtUserEmail.Text = "";
txtUserId.Focus();
}
else
{
loginlink.Visible = false;
lblError.ForeColor = Color.Red;
lblError.Text = "*User does not exist in domain!!!";
}
}
private bool UserExists(string uname, string domain)
{
using (var pc = new PrincipalContext(ContextType.Domain, domain))
using (var p = Principal.FindByIdentity(pc, IdentityType.SamAccountName, uname))
{
return p != null;
}
}
}
}