概念问题 Application_BeginRequest 中Context.User.Identity.Name

hardVB 2003-04-29 11:00:02
Line 26: void Application_BeginRequest(Object sender, EventArgs e) {
Line 27: Response.Write("User.Identity.Name");
Line 28: Response.Write(Context.User.Identity.Name);
Line 29: Response.End

错误为Line 28
Object reference not set to an instance of an object.

Context是什么冬冬?
...全文
139 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinchy 2003-05-10
  • 打赏
  • 举报
回复
hardVB 2003-04-29
  • 打赏
  • 举报
回复
哪位师兄帮帮忙看看?
hardVB 2003-04-29
  • 打赏
  • 举报
回复
这是代码,请看看Context哪里初始化?

<%@ Import Namespace="ASPNetPortal" %>
<%@ Import Namespace="System.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">

//*********************************************************************
//
// Application_BeginRequest Event
//
// The Application_BeginRequest method is an ASP.NET event that executes
// on each web request into the portal application. The below method
// obtains the current tabIndex and TabId from the querystring of the
// request -- and then obtains the configuration necessary to process
// and render the request.
//
// This portal configuration is stored within the application's "Context"
// object -- which is available to all pages, controls and components
// during the processing of a single request.
//
//*********************************************************************



void Application_BeginRequest(Object sender, EventArgs e) {


int tabIndex = 0;
int tabId = 0;

// Get TabIndex from querystring

if (Request.Params["tabindex"] != null) {
tabIndex = Int32.Parse(Request.Params["tabindex"]);
}

// Get TabID from querystring

if (Request.Params["tabid"] != null) {
tabId = Int32.Parse(Request.Params["tabid"]);
}

Context.Items.Add("PortalSettings", new PortalSettings(tabIndex, tabId));
}

//*********************************************************************
//
// Application_AuthenticateRequest Event
//
// If the client is authenticated with the application, then determine
// which security roles he/she belongs to and replace the "User" intrinsic
// with a custom IPrincipal security object that permits "User.IsInRole"
// role checks within the application
//
// Roles are cached in the browser in an in-memory encrypted cookie. If the
// cookie doesn't exist yet for this session, create it.
//
//*********************************************************************

void Application_AuthenticateRequest(Object sender, EventArgs e) {



if (Request.IsAuthenticated == true) {

String[] roles;

// Create the roles cookie if it doesn't exist yet for this session.
if ((Request.Cookies["portalroles"] == null) || (Request.Cookies["portalroles"].Value == "")) {

// Get roles from UserRoles table, and add to cookie
UsersDB user = new UsersDB();
roles = user.GetRoles(User.Identity.Name);
Response.Write("User.Identity.Name:");
Response.Write(User.Identity.Name);
Response.End();
// Create a string to persist the roles
String roleStr = "";
foreach (String role in roles) {
roleStr += role;
roleStr += ";";
}

// Create a cookie authentication ticket.
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // version
Context.User.Identity.Name, // user name
DateTime.Now, // issue time
DateTime.Now.AddHours(1), // expires every hour
false, // don't persist cookie
roleStr // roles
);

// Encrypt the ticket
String cookieStr = FormsAuthentication.Encrypt(ticket);

// Send the cookie to the client
Response.Cookies["portalroles"].Value = cookieStr;
Response.Cookies["portalroles"].Path = "/";
Response.Cookies["portalroles"].Expires = DateTime.Now.AddMinutes(1);
}
else {

// Get roles from roles cookie
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies["portalroles"].Value);

//convert the string representation of the role data into a string array
ArrayList userRoles = new ArrayList();

foreach (String role in ticket.UserData.Split( new char[] {';'} )) {
userRoles.Add(role);
}

roles = (String[]) userRoles.ToArray(typeof(String));
}

// Add our own custom principal to the request containing the roles in the auth ticket
Context.User = new GenericPrincipal(Context.User.Identity, roles);
}
}

</script>
hardVB 2003-04-29
  • 打赏
  • 举报
回复
在哪里初始化?
hardVB 2003-04-29
  • 打赏
  • 举报
回复
如何查看Context 的ClassName
timmy3310 2003-04-29
  • 打赏
  • 举报
回复
Context是请求的上下文

你在这里使用Context属性是不行的,这时候Context属性还没有初始化
hardVB 2003-04-29
  • 打赏
  • 举报
回复
补充:以上在global.asax

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧