aspx代码
我不知道是否有更好的方法,但是这是我的方法:
mypage.aspx:
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Data.OracleClient" %>
<script runat="server" src="oralib.cs"></script>
<html>
<head><title>Reusable Code sample</title>
</head>
<body>
<%
OraLib ora = new OraLib(Request);
if ( ora.HasOneRole("'IMAGE_UPLOAD'") ) {
%><H3>Welcome</H3>
<%
} else {
%><em><b><font color="red">UNAUTHORIZED</font></b></em>
<%
}
ora.Close();
%></body>
</html>
oralib.cs:
public class OraLib {
OracleConnection conn;
HttpRequest req;
bool isopen;
public OraLib(HttpRequest hrq) {
req = hrq;
conn = new OracleConnection("user id=/;data source=myoradb;Integrated Security=true");
conn.Open();
isopen = true;
}
public void Close() {
if ( isopen ) {
conn.Close();
}
isopen = false;
}
public bool HasOneRole(String rol) {
String uid = req.ServerVariables["AUTH_USER"].ToUpper();
OracleCommand ocmd = conn.CreateCommand();
ocmd.CommandText = "select count(*) from dba_role_privs where grantee='"+uid+"' and granted_role in ("+rol+")";
OracleDataReader rows = ocmd.ExecuteReader();
int rval = 0;
if ( rows.Read() ) {
rval = rows.GetInt32(0);
}
rows.Close();
return (rval!=0);
}
}
注意:
在.aspx页上,您需要@导入包含的.cs文件使用的名称空间。 在上面的示例中,oralib.cs使用OracleClient命名空间,因此.aspx会在脚本src标记引入oralib.cs之前导入那些名称。
我希望这对某人有帮助,或者如果其他人有更好的主意,请随时发表评论...
翻译自: https://bytes.com/topic/asp-net/insights/951303-how-share-code-between-aspx-pages
aspx代码