用户不同的登录控制 [英] User difined login controle

查看:78
本文介绍了用户不同的登录控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







如果存在于我的SQL数据库表中,我想检查UserName和PAssword并验证用户是否登录我想在Sessions中存储用户信息,当用户点击注销时我想让会话到期如何才能实现这个目的?

Hi,


I want to check UserName and PAssword and validate the user for login if exists in my SQL database table and i want to store user information in Sessions and when user clicks logout i want to expire the sessions how can i achieve this?

推荐答案

只需将Login控件拖放到网页并双击它并编写代码。

Login.aspx(源代码):

Simply drag and drop the Login control in web page and double click on it and write the code.
Login.aspx (source code):
<![CDATA[<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 

AutoEventWireup="true"

CodeFile="Login.aspx.cs" Inherits="Login" %>]]>
<asp:content id="Content1" contentplaceholderid="head" runat="Server" xmlns:asp="#unknown">
<style type="text/css">
.style2
{
font-size: large;
}
</style>
</asp:content>
<asp:content id="Content2" contentplaceholderid="ContentPlaceHolder1" runat="Server" xmlns:asp="#unknown">
<div>
<h2 style="color: Green">
Login in ASP.NET 4, C#
</h2>
<asp:login id="Login1" runat="server" backcolor="#F7F6F3" bordercolor="#E6E2D8">
BorderPadding="4"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"
ForeColor="#333333" Height="192px" Style="font-size: medium" Width="308px" 
onauthenticate="Login1_Authenticate">
<instructiontextstyle font-italic="True" forecolor="Black" />
<loginbuttonstyle backcolor="#FFFBFF" bordercolor="#CCCCCC" borderstyle="Solid">
BorderWidth="1px"
Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<textboxstyle font-size="0.8em" />
<titletextstyle backcolor="#5D7B9D" font-bold="True" font-size="0.9em">
ForeColor="White" />
</titletextstyle></loginbuttonstyle></asp:login>
</div>
</asp:content>





Login.aspx.cs(C#代码文件):



Login.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login1.UserName;
string pwd = Login1.Password;
string s;
s = WebConfigurationManager.ConnectionStrings["ChartDatabaseConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(s);
con.Open();
string sqlUserName;
sqlUserName = "SELECT user_name,password FROM Login WHERE user_name ='" + username + "' AND password ='" + pwd + "'";
SqlCommand cmd = new SqlCommand(sqlUserName, con);
string CurrentName;
CurrentName = (string)cmd.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = username;
Session.Timeout = 1;
Response.Redirect("start.aspx");
}
else
{
Session["UserAuthentication"] = "";
}
}
}


这篇关于用户不同的登录控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆