I am trying to get the user's SID by determining who is logged in and
storing that into a cookie. I am trying to do this inside the
global.asax file, but it breaks when it tries to create the SPWeb
object. Can someone please tell me what I am doing wrong?
<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Application Language="C#"
Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %
>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
//Create a new cookie, passing the name into the constructor
HttpCookie cookie = new HttpCookie("MOSS_USER");
SPWeb mySite = SPControl.GetContextWeb(Context);
SPUser myUser = mySite.CurrentUser;
string strUserSID = myUser.Sid;
//Set the cookies value
cookie.Value = strUserSID;
//Set the cookie to expire in 1 minute
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(1, 0, 0, 0);
cookie.Expires = dtNow + tsMinute;
//Add the cookie
Response.Cookies.Add(cookie);
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the
sessionstate mode
// is set to InProc in the Web.config file. If session mode
is set to StateServer
// or SQLServer, the event is not raised.
}
</script>