Basf_FCL/FCL/FineUIPro.Web/Login.aspx.cs

299 lines
12 KiB
C#
Raw Normal View History

2024-05-08 10:17:02 +08:00
using System;
using System.Web.UI;
using BLL;
using System.Linq;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
using System.Configuration;
using System.Web.Services;
namespace FineUIPro.Web
{
public partial class Login : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
[WebMethod]
public static string LoginPost(string user, string pwd)
{
return new Login().btnLogin_Click(user, pwd);
}
private string btnLogin_Click(string account, string pwd)
{
string url = "";
string userNameProxyUser = "CN=byc_fcl,ou=Proxy-User,ou=EMEA,o=SERVICES";
string passwordProxyUser = "3gLFbB5E";
string domain = "bgd-ldap-lb.rz-c007-j650.basf-ag.de";
string path = "o=AUTH";
//string serial = ConfigurationManager.AppSettings["Serial"];
string serial = "612d524b000000000002";
bool result = false;
try
{
LdapUser ldapuser = new LdapUser();
result = AuthenticateUser(account, pwd, userNameProxyUser, passwordProxyUser, domain, path, serial,out ldapuser);
if (result)
{
var user = from x in Funs.DB.Sys_User where x.Account == account select x;
if (user.Count() == 0)
{
Model.Sys_User newUser = new Model.Sys_User();
newUser.UserId = SQLHelper.GetNewID(typeof(Model.Sys_User));
newUser.Account = account;
//newUser.UserName = txtUserName.Text.Trim();
//newUser.Email = txtEmail.Text.Trim();
newUser.UserName = ldapuser.FullName;
newUser.Email = ldapuser.EmailAddress;
newUser.RoleId = Const.Role_CommonUsers;
newUser.IsPost = true;
Funs.DB.Sys_User.InsertOnSubmit(newUser);
Funs.DB.SubmitChanges();
//Alert.Show("验证通过! "+ txtUserName.Text+"|"+ txtEmail.Text.Trim(), MessageBoxIcon.Success);
}
else
{
// 更新用户名和邮箱
if (string.IsNullOrEmpty(user.First().UserName) || string.IsNullOrEmpty(user.First().Email))
{
Model.Sys_User newUser = Funs.DB.Sys_User.FirstOrDefault(x => x.UserId == user.First().UserId);
if (newUser != null)
{
newUser.UserName = ldapuser.FullName;
newUser.Email = ldapuser.EmailAddress;
//newUser.UserName = txtUserName.Text.Trim();
//newUser.Email = txtEmail.Text.Trim();
Funs.DB.SubmitChanges();
}
}
}
if (BLL.Sys_UserService.ADUserLogOn(account, true, this.Page) == true)
{
url = "default.htm";
//Response.Redirect("~/default.htm");
}
}
else
{
if (BLL.Sys_UserService.UserLogOn(account, pwd, true, this.Page))
{
url = "default.htm";
//Response.Redirect("~/default.htm");
}
else
{
Alert.Show("验证未通过", MessageBoxIcon.Success);
}
}
}
catch (Exception ex)
{
Alert.Show(ex.ToString(), MessageBoxIcon.Success);
}
return url;
}
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//protected void btnLogin_Click(object sender, EventArgs e)
//{
// string userName = tbxUserName.Text.Trim();
// string paswword = tbxPassword.Text.Trim();
// bool rememberMe = this.savemessgae.Checked;
// //////////////////////////////////////////////////////////////////////////////////////////////
// string userNameProxyUser = "cn=byc_user,ou=proxy-user,ou=emea,o=services";
// string passwordProxyUser = "eTi97Jw7";
// string domain = "bgd-ldap-lb.rz-c007-j650.basf-ag.de:636";
// string path = "o=AUTH";
// //string serial = ConfigurationManager.AppSettings["Serial"];
// string serial = "7e00000004870a85c98afc0a2d000000000004";
// bool result = false;
// try
// {
// result = AuthenticateUser(userName, paswword, userNameProxyUser, passwordProxyUser, domain, path, serial);
// if (result)
// {
// var user = from x in Funs.DB.Sys_User where x.Account == userName select x;
// if (user.Count() == 0)
// {
// Model.Sys_User newUser = new Model.Sys_User();
// newUser.UserId = SQLHelper.GetNewID(typeof(Model.Sys_User));
// newUser.Account = userName;
// newUser.UserName = txtUserName.Text.Trim();
// newUser.Email = txtEmail.Text.Trim();
// newUser.RoleId = Const.Role_CommonUsers;
// newUser.IsPost = true;
// Funs.DB.Sys_User.InsertOnSubmit(newUser);
// Funs.DB.SubmitChanges();
// //Alert.Show("验证通过! "+ txtUserName.Text+"|"+ txtEmail.Text.Trim(), MessageBoxIcon.Success);
// }
// else
// {
// // 更新用户名和邮箱
// if (string.IsNullOrEmpty(user.First().UserName) || string.IsNullOrEmpty(user.First().Email))
// {
// Model.Sys_User newUser = Funs.DB.Sys_User.FirstOrDefault(x => x.UserId == user.First().UserId);
// if (newUser != null)
// {
// newUser.UserName= txtUserName.Text.Trim();
// newUser.Email= txtEmail.Text.Trim();
// Funs.DB.SubmitChanges();
// }
// }
// }
// if (BLL.Sys_UserService.ADUserLogOn(userName, rememberMe, this.Page) == true)
// {
// Response.Redirect("~/default.htm");
// }
// }
// else
// {
// if (BLL.Sys_UserService.UserLogOn(userName, paswword, rememberMe, this.Page))
// {
// Response.Redirect("~/default.htm");
// }
// else
// {
// Alert.Show("验证未通过", MessageBoxIcon.Success);
// return;
// }
// }
// }
// catch (Exception ex)
// {
// Alert.Show(ex.ToString(), MessageBoxIcon.Success);
// return;
// }
// ///////////////////////////////////////////////////////////////////////////////////////////
// //BLL.LdapAuthentication.IsConnected(BLL.Funs.LdapUrl, userName, paswword);
// //string adPath = "LDAP://" + BLL.Funs.LdapUrl;
// //LdapAuthentication ladp = new LdapAuthentication(adPath);
// //if (ladp.IsAuthenticated(BLL.Funs.LdapUrl, userName, paswword) == true)
// //{
// // string userInfo = ladp.GetADUserInfo(userName);
// // //测试获取用户信息
// // ScriptManager.RegisterStartupScript(this, typeof(string), "_alert", "alert('" + userInfo + "')", true);
// // if (!string.IsNullOrEmpty(userInfo))
// // {
// // string[] users = userInfo.Split('|');
// // }
// // if (BLL.Sys_UserService.ADUserLogOn(userName, rememberMe, Page))
// // {
// // PageBase.ZXRefresh(ResolveUrl("~/default.htm"));
// // }
// //}
// //else if (BLL.Sys_UserService.UserLogOn(userName, paswword, rememberMe, this.Page))
// //{
// // PageBase.ZXRefresh(ResolveUrl("~/default.htm"));
// //}
// //else
// //{
// // ScriptManager.RegisterStartupScript(this, typeof(string), "_alert", "alert('The account does not match the password!')", true);
// // //Alert.ShowInTop("用户名与密码不匹配!", MessageBoxIcon.Warning);
// //}
// //////////////////////////////////////////////////////////////////////////////////////////////////
// //if (BLL.Sys_UserService.UserLogOn(userName, paswword, rememberMe, this.Page))
// //{
// // //BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "登陆成功!");
// // PageBase.ZXRefresh(ResolveUrl("~/default.htm"));
// //}
// //else
// //{
// // ScriptManager.RegisterStartupScript(this, typeof(string), "_alert", "alert('The account does not match the password!')", true);
// // //Alert.ShowInTop("用户名与密码不匹配!", MessageBoxIcon.Warning);
// //}
//}
public bool AuthenticateUser(string userName,
string password,
string ldapUserName,
string ldapPassword,
string ldapDomain,
string ldapPath,
string ldapSslSerialKey, out LdapUser ldapUser)
{
bool isVaild = false;
ldapUser = null;
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
{
// momentan in Testversion
//Bis.SK.Ldap.LdapQuery query = new LdapQuery(ldapUserName, ldapPassword, ldapDomain, ldapPath);
//return query.CheckPassword(userName, password);
LdapUser user = new LdapUser();
LdapAuthentication auth = new LdapAuthentication();
auth.UserNameProxyUser = ldapUserName;
auth.PasswordProxyUser = ldapPassword;
auth.Domain = ldapDomain;
auth.Path = ldapPath;
auth.UseSecureSocketLayer = true;
auth.VerifyCertificateLocally = true;
auth.AllowAnyCertificates = true;
auth.LocallyVerificationCertificate = null;
auth.LocallyVerificationCertificateSerialNumber = ldapSslSerialKey;
if (userName.IndexOf('\\') > 0)
{
userName = userName.Split('\\')[1];
}
try
{
isVaild = auth.CheckPassword(userName, password, out user);
ldapUser = user;
//txtUserName.Text = user.FullName;
//txtEmail.Text = user.EmailAddress;
}
catch (Exception ex)
{
isVaild = false;
}
}
return isVaild;
}
}
}