在用户使用信息系统过程中
有时候 某些功能
上级用户可能希望
下级用户需要首先得到其授权后
方可使用
下面提供一种
生成随机但又有规律可循的一组问答数
以提供远程授权服务 的简单示例
示例页面如下:
操作流程如下:
下级用户进入需要上级授权才能保存的某个页面
页面会提供一组随机码
在保存等功能操作前
下级用户需要先将这组随机码告知上级用户
上级用户进入授权解码页面
利用这组随机码得到授权码
并告知下级用户
下级用户在保存等操作前
输入这组验证码
如果验证码正确
则下级用户的保存等操作可顺利进行
否则失败
相关代码如下:
RandomKey.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RandomKey.aspx.cs" Inherits="RandomKey" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>生成随机但又有规律可循的一组问/答数 以提供远程授权服务</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td colspan="2">用户浏览者(下级用户) 得到授权后 方可进行保存等功能操作的页面</td>
</tr>
<tr>
<td>本机本次保存操作的随机码:</td>
<td>
<asp:Label ID="lbl_LocalQuestionRandomKey" runat="server"></asp:Label></td>
</tr>
<tr>
<td>从网站客服(上级用户)处取得的授权/验证码:</td>
<td>
<asp:TextBox ID="txt_RandomKey" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btn_Save" runat="server" Text="保存操作" OnClick="btn_Save_Click" />
</td>
</tr>
</table>
<br />
<hr />
<table>
<tr>
<td colspan="2">
网站客服(上级用户) 所能操作 的 根据随机码得到验证码 的页面
</td>
</tr>
<tr>
<td>客户(下级用户)提供的随机码:</td>
<td><asp:TextBox ID="txt_ClientRandomKey" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btn_GetRightRandomKey" runat="server" Text="得到授权/验证码" OnClick="btn_GetRightRandomKey_Click" />
</td>
</tr>
<tr>
<td>
返回给客户(下级用户)的授权/验证码:
</td>
<td>
<asp:TextBox ID="txt_ReturnToClientRandomKey" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RandomKey.aspx.cs" Inherits="RandomKey" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>生成随机但又有规律可循的一组问/答数 以提供远程授权服务</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td colspan="2">用户浏览者(下级用户) 得到授权后 方可进行保存等功能操作的页面</td>
</tr>
<tr>
<td>本机本次保存操作的随机码:</td>
<td>
<asp:Label ID="lbl_LocalQuestionRandomKey" runat="server"></asp:Label></td>
</tr>
<tr>
<td>从网站客服(上级用户)处取得的授权/验证码:</td>
<td>
<asp:TextBox ID="txt_RandomKey" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btn_Save" runat="server" Text="保存操作" OnClick="btn_Save_Click" />
</td>
</tr>
</table>
<br />
<hr />
<table>
<tr>
<td colspan="2">
网站客服(上级用户) 所能操作 的 根据随机码得到验证码 的页面
</td>
</tr>
<tr>
<td>客户(下级用户)提供的随机码:</td>
<td><asp:TextBox ID="txt_ClientRandomKey" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btn_GetRightRandomKey" runat="server" Text="得到授权/验证码" OnClick="btn_GetRightRandomKey_Click" />
</td>
</tr>
<tr>
<td>
返回给客户(下级用户)的授权/验证码:
</td>
<td>
<asp:TextBox ID="txt_ReturnToClientRandomKey" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</html>
RandomKey.aspx.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class RandomKey : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//生成本机本次保存操作的随机码
this.lbl_LocalQuestionRandomKey.Text = QuestionAnswerRandomKey.GetQuestionRandomKey();
}
}
protected void btn_Save_Click(object sender, EventArgs e)
{
if (this.txt_RandomKey.Text.Trim() == "")
{
fn_AlertInfo("失败");
return;
}
else
{
//问题码
string strLocalQuestionRandomKey = this.lbl_LocalQuestionRandomKey.Text.Trim();
//答案码
string strAnswerRandomKey = QuestionAnswerRandomKey.GetAnswerRandomKey(strLocalQuestionRandomKey);
if (this.txt_RandomKey.Text.Trim() == strAnswerRandomKey)
{
//通过
fn_AlertInfo("通过");
}
else
{
//错误
fn_AlertInfo("失败");
}
}
}
弹出相关提示#region 弹出相关提示
private void fn_AlertInfo(string strMsg)
{
string strAlert = "<script type='text/javascript'> alert('" + strMsg + "'); </script> ";
Page.ClientScript.RegisterStartupScript( Page.GetType(),Guid.NewGuid().ToString(), strAlert);
}
#endregion
客服用 由客户提供的随机码得到其对应的验证码#region 客服用 由客户提供的随机码得到其对应的验证码
protected void btn_GetRightRandomKey_Click(object sender, EventArgs e)
{
if(this.txt_ClientRandomKey.Text!="")
{
this.txt_ReturnToClientRandomKey.Text = QuestionAnswerRandomKey.GetAnswerRandomKey(this.txt_ClientRandomKey.Text.Trim());
}
}
#endregion
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class RandomKey : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//生成本机本次保存操作的随机码
this.lbl_LocalQuestionRandomKey.Text = QuestionAnswerRandomKey.GetQuestionRandomKey();
}
}
protected void btn_Save_Click(object sender, EventArgs e)
{
if (this.txt_RandomKey.Text.Trim() == "")
{
fn_AlertInfo("失败");
return;
}
else
{
//问题码
string strLocalQuestionRandomKey = this.lbl_LocalQuestionRandomKey.Text.Trim();
//答案码
string strAnswerRandomKey = QuestionAnswerRandomKey.GetAnswerRandomKey(strLocalQuestionRandomKey);
if (this.txt_RandomKey.Text.Trim() == strAnswerRandomKey)
{
//通过
fn_AlertInfo("通过");
}
else
{
//错误
fn_AlertInfo("失败");
}
}
}
弹出相关提示#region 弹出相关提示
private void fn_AlertInfo(string strMsg)
{
string strAlert = "<script type='text/javascript'> alert('" + strMsg + "'); </script> ";
Page.ClientScript.RegisterStartupScript( Page.GetType(),Guid.NewGuid().ToString(), strAlert);
}
#endregion
客服用 由客户提供的随机码得到其对应的验证码#region 客服用 由客户提供的随机码得到其对应的验证码
protected void btn_GetRightRandomKey_Click(object sender, EventArgs e)
{
if(this.txt_ClientRandomKey.Text!="")
{
this.txt_ReturnToClientRandomKey.Text = QuestionAnswerRandomKey.GetAnswerRandomKey(this.txt_ClientRandomKey.Text.Trim());
}
}
#endregion
}
App_Code下的
QuestionAnswerRandomKey.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/**//// <summary>
/// QuestionAnswerRandomKey 生成一组类似锁和钥匙的一对问答随机码
/// </summary>
public class QuestionAnswerRandomKey
{
public QuestionAnswerRandomKey()
{
}
得到一组1至99999的数字的随机码#region 得到一组1至99999的数字的随机码
public static string GetQuestionRandomKey()
{
Random ra = new Random();
return ra.Next(1, 99999).ToString();
}
#endregion
根据随机码 通过一定算法规则 得到验证码#region 根据随机码 通过一定算法规则 得到验证码
public static string GetAnswerRandomKey(string strQuestionRandomKey)
{
int intQuestion = Convert.ToInt32(strQuestionRandomKey);
int intAnswerRandomKey = 0;
//一定的算法规则
int intDay = DateTime.Today.DayOfYear;
intAnswerRandomKey = intQuestion + intDay;
intAnswerRandomKey = intAnswerRandomKey + 5000;
intAnswerRandomKey = intAnswerRandomKey / 7;
intAnswerRandomKey = intAnswerRandomKey * 3;
intAnswerRandomKey = intAnswerRandomKey - 21;
if (intAnswerRandomKey < 1)
intAnswerRandomKey = intAnswerRandomKey + 37245;
return intAnswerRandomKey.ToString();
}
#endregion
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/**//// <summary>
/// QuestionAnswerRandomKey 生成一组类似锁和钥匙的一对问答随机码
/// </summary>
public class QuestionAnswerRandomKey
{
public QuestionAnswerRandomKey()
{
}
得到一组1至99999的数字的随机码#region 得到一组1至99999的数字的随机码
public static string GetQuestionRandomKey()
{
Random ra = new Random();
return ra.Next(1, 99999).ToString();
}
#endregion
根据随机码 通过一定算法规则 得到验证码#region 根据随机码 通过一定算法规则 得到验证码
public static string GetAnswerRandomKey(string strQuestionRandomKey)
{
int intQuestion = Convert.ToInt32(strQuestionRandomKey);
int intAnswerRandomKey = 0;
//一定的算法规则
int intDay = DateTime.Today.DayOfYear;
intAnswerRandomKey = intQuestion + intDay;
intAnswerRandomKey = intAnswerRandomKey + 5000;
intAnswerRandomKey = intAnswerRandomKey / 7;
intAnswerRandomKey = intAnswerRandomKey * 3;
intAnswerRandomKey = intAnswerRandomKey - 21;
if (intAnswerRandomKey < 1)
intAnswerRandomKey = intAnswerRandomKey + 37245;
return intAnswerRandomKey.ToString();
}
#endregion
}