private void Button1_Click(object sender, System.EventArgs e) { //Write your own Authentication logic here if(this.username.Text != "" && this.password.Text !="") { //Write your own code to get the User Roles ArrayList roles = new ArrayList(); roles.Add("Manager"); if(this.username.Text == "superuser") roles.Add("Administrator"); roles.Add("ITUser"); //Convert roles into pipe "|" separated string System.Text.StringBuilder strRoles = new System.Text.StringBuilder(); foreach(string role in roles) { strRoles.Append(role); strRoles.Append("|"); } CustomIdentity userIdentity = new CustomIdentity(this.username.Text, 1, true, true, this.username.Text, "*****@*****.**", strRoles.ToString()); CustomPrincipal principal = new CustomPrincipal(userIdentity, roles); Context.User = principal; //string estr = CustomAuthentication.Encrypt(userIdentity); CustomAuthentication.RedirectFromLoginPage(userIdentity); } }
void OnAuthenticate(object sender, EventArgs e) { app = (HttpApplication)sender; HttpRequest req = app.Request; HttpResponse res = app.Response; string loginUrl = ConfigurationSettings.AppSettings[LOGINURL_KEY]; if(loginUrl == null || loginUrl.Trim() == String.Empty) { throw new Exception(" CustomAuthentication.LoginUrl entry not found in appSettings section of Web.config"); } string cookieName = ConfigurationSettings.AppSettings[AUTHENTICATION_COOKIE_KEY]; if(cookieName == null || cookieName.Trim() == String.Empty) { throw new Exception(" CustomAuthentication.Cookie.Name entry not found in appSettings section section of Web.config"); } int i = req.Path.LastIndexOf("/"); string page = req.Path.Substring(i+1, (req.Path.Length - (i + 1))); int j = loginUrl.LastIndexOf("/"); string loginPage = loginUrl.Substring(j+1, (loginUrl.Length - (j + 1))); if(page != null && !(page.Trim().ToUpper().Equals(loginPage.ToUpper()))) { if(req.Cookies.Count > 0 && req.Cookies[cookieName.ToUpper()] != null) { HttpCookie cookie = req.Cookies[cookieName.ToUpper()]; if(cookie != null) { string str = cookie.Value; CustomIdentity userIdentity = CustomAuthentication.Decrypt(str); string[] roles = userIdentity.UserRoles.Split(new char[]{'|'}); ArrayList arrRoles = new ArrayList(); arrRoles.InsertRange(0, roles); CustomPrincipal principal = new CustomPrincipal(userIdentity, arrRoles); app.Context.User = principal; Thread.CurrentPrincipal = principal; } } else { res.Redirect(req.ApplicationPath + loginUrl + "?ReturnUrl=" + req.Path, true); } } }
private void MainForm_Load(object sender, System.EventArgs e) { // Получаем имя пользователя. LoginForm login = new LoginForm(); if (login.ShowDialog() != DialogResult.OK) { // Если пользователь отменил ввод имени, закрываем приложение this.Close(); return; } try { // Создаем объекты IIdentity и IPrincipal CustomIdentity identity = new CustomIdentity(login.txbUserName.Text); CustomPrincipal principal = new CustomPrincipal(identity); Thread.CurrentPrincipal = principal; this.ValidateUser(); } catch (Exception ex) { // Обрабатываем исключения и закрываем приложение. MessageBox.Show("Во время выполнения приложения возникла ошибка: " + ex.Message, "Ошибка"); this.Close(); return; } }