在没有存储过程帮助的情况下如何进行登录? [英] How to work login without the help of stored procedure?

查看:68
本文介绍了在没有存储过程帮助的情况下如何进行登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有存储过程帮助的情况下进行登录?

How to work login without the help of stored procedure?

推荐答案

有几种方法可以做到这一点

如果您拥有数据库,则可以进行存储过程或动态查询.

您还可以使用文件存储用户凭据,以及当用户登录系统时从文件中读取数据并验证用户身份.
There are several ways to do this

if you are having database then you can go for stored procedure or dynamic queries.

You can also use file to store user credentials and when user login to the system read data from file and authenticate user.


您是否正在寻找类似这样的内容:
Are you looking for something like this: User Login With XML...[^]

Since above one is in Vb, converting important snippets in C# for you.
1. Place ASP.NET Login Control on a web page.

2. XML File:
<?xml version="1.0" encoding="utf-8" ?>
<Staff>
  <User>
    <username>aspxcode</username>
    <password>2008</password>
  </User>
    <User>
    <username>windows</username>
    <password>vista</password>
  </User>
</Staff>


3.背后的代码:


3. Code behind:

protected void Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
{
	string username = null;
	string pwd = null;
	string CurrentUser = "";
	string CurrentPwd = "";
	bool LoginStatus = false;

	username = Login1.UserName;
	pwd = Login1.Password;

	XmlDocument xd = new XmlDocument();
	xd.Load(Server.MapPath("~/App_Data/AllUser.xml"));

	XmlNodeList xnl = xd.GetElementsByTagName("User");

	foreach (XmlNode xn in xnl) {
		XmlNodeList cxnl = xn.ChildNodes;
		foreach (XmlNode cxn in cxnl) {
			if (cxn.Name == "username") {
				if (cxn.InnerText == username) {
					CurrentUser = username;
				}
			}

			if (cxn.Name == "password") {
				if (cxn.InnerText == pwd) {
					CurrentPwd = pwd;
				}
			}
		}

		if ((!string.IsNullOrEmpty(CurrentUser)) & (!string.IsNullOrEmpty(CurrentPwd))) {
			LoginStatus = true;
		}
	}

	if (LoginStatus == true) {
		Session("UserAuthentication") = username;
		Session.Timeout = 1;

		Response.Redirect("how-to-StartPage.aspx");
	} else {
		Session("UserAuthentication") = "";
	}
}



尝试!



Try!


登录成功后,我制作了一个用户令牌并将其保存到文件中. 如果注销,则删除文件.

例如

I made a user token and saved it to a file, when login is successful.
if log-out, delete file.

eg

public class UserToken
{
    private int UserID; //ContactID
    private string MagicString;
    private static Random Generator = new Random();
    private int MAX = (int) Math.Pow(2,16);

//if user returns or send request check if file still exists
//else show login screen...

private void GenerateMagicToken()
    {
        MagicString = string.Empty;
        int MagicInt = Generator.Next(MAX);
        MagicString = MagicInt.ToString();
        //write to file
        string path = Folder + UserID.ToString() +".token";
        if (File.Exists(path)) //prevent more than one token per user- ie Login only once!!!
        {
            throw new Exception("User is already Logged in!");
        }
        else
        {
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(MagicString);
                
            }
        }
    }

}


这篇关于在没有存储过程帮助的情况下如何进行登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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