English 中文(简体)
页: 1
原标题:Cookies and session in asp.net

I am creating a login and the storing the user details in a cookie using this code

if (ValidateUser(txtUserName.Value,txtUserPass.Value) )
{
   //string useremail = Convert.ToString(txtUserName.Value);
   Session.Add("useremail", txtUserName.Value);
   FormsAuthenticationTicket tkt;
   string cookiestr;
   HttpCookie ck;
   tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now, 
     DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");
   cookiestr = FormsAuthentication.Encrypt(tkt);
   ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
   if (chkPersistCookie.Checked)
     ck.Expires=tkt.Expiration; 
   ck.Path = FormsAuthentication.FormsCookiePath; 
   Response.Cookies.Add(ck);
}

I am also creating a session Session.Add("useremail", txtUserName.Value); After succesfull authentication it is redirected to user.aspx I want to read the useremail value in the user.aspx page but when I tried to access the value in the user page it is not showing useremail field.

protected void Page_Load(object sender, EventArgs e)
    {
        if
            (Session["useremail"] == null) Response.Redirect("Home.aspx");
        else

            BindGridView(useremail);
    }

这是我的网络会议:

<authentication mode="Forms"><forms name=".YAFNET_Authentication" loginUrl="Home.aspx" protection="All" timeout="43200" cookieless="UseCookies"/></authentication>

如果我做错的话,我会说话。 并请告诉我,如何将使用电子邮件的价值转给用户。 as 页,以便我能够把这一价值传给电网功能

最佳回答

改写

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["useremail"] == null)
            Response.Redirect("Home.aspx");
        else
            BindGridView((string)Session["useremail"]);
    }
问题回答

您可向本届会议提出反对,如:

Session["useremail"] = "john.smith@microsoft.com";

然后,你可以采取以下方式检索:

var useremail = Session["useremail"] ?? null;
if (useremail == null)
{
   //...
}
else
{
    BindGridView(useremail);
}

如果会议没有“用户邮件”项目,则使用电子邮件变数将予撤销,它将包含电子邮件地址。

You are getting confused with relationship between authentication, session state and cookies. In ASP.NET, Session State and Forms Authentication are not linked i.e. their scope are different. You can have some session state for un-authenticated user. Session and forms authentication uses different cookies for tracking purposes and the cookie management is more or less automatic and you don t really need to write code to manage it as you have done. Besides, what you store in the cookie has no bearing on what goes in the session state. Its also possible to have both session and forms authentication to get working w/o cookies. So code such as below should work for session state

Session["key"] = "put your data here";

// retrieve the data elsewhere
var data = Session["key"];




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签