ASP.NET成员资格:如何设置用户的登录 [英] ASP.NET Membership: how to set the user as logged in

查看:94
本文介绍了ASP.NET成员资格:如何设置用户的登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得成员资格提供工作。

到目前为止,我有:

 < ASP:登录ID =Login1=服务器OnAuthenticate =Login1_Authenticate>
 < / ASP:登录>

电话:

 保护无效Login1_Authenticate(对象发件人,AuthenticateEventArgs E)
{
    如果(Membership.ValidateUser(Login1.UserName,Login1.Password))
    {
        的Response.Redirect(/管理/ Default.aspx的);
        //为登录设置用户?
    }
}

如果我输入正确的登录/密码,则ValidateUser函数,以便返回true。所以我的问题是:我怎么设置用户的登录

我在我的网页测试该做的:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(Membership.GetUser()== NULL)
    {
        的Response.Redirect(/管理/ login.aspx的);
    }
    //否则你登录了,祝贺你
}

我会用默认的功能,但它只是不工作,谷歌搜索让我觉得,我将通过实际重新编写所有自己节省时间。

任何帮助!

修改:对于接受的答案,这是正确的为如何设置用户为登录,并工作正常。它没有固定的我的特定问题,但只是其中的一部分。心想如果你看看认为的意见,你会发现有趣的指针。

编辑2及解决方法:好吧,我终于摸索出来感谢所有的意见。下面是我做的,这比我预期的更简单:

页面,检查登录状态:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
 {
     如果(!Request.IsAuthenticated)
     {
         的Response.Redirect(/管理/ login.aspx的);
     }

注销

 保护无效LoginStatus1_Logout(对象发件人,LoginCancelEventArgs E)
   {
       FormsAuthentication.SignOut();
       的Response.Redirect(/管理/ login.aspx的);
   }
}

web.config中:

 <身份验证模式=表格/>

登录

 保护无效Login1_Authenticate(对象发件人,AuthenticateEventArgs E)
{
    如果(Membership.ValidateUser(Login1.UserName,Login1.Password))
    {
        FormsAuthentication.SetAuthCookie(Login1.UserName,真);
        的Response.Redirect(/管理/ Default.aspx的);    }
}


解决方案

在调用之前将这个在 Login1_Authenticate 的Response.Redirect(/管理/ Default.aspx的);

  FormsAuthentication.SetAuthCookie(用户名,真正的);

I am trying to get the Membership Provider to work.

So far I have:

 <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
 </asp:Login>

calling :

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    if(Membership.ValidateUser(Login1.UserName, Login1.Password))
    {
        Response.Redirect("/admin/default.aspx");
        // Set the user as logged in?
    }
}

If I enter the correct login/password, the ValidateUser function returns true. So my question is: how do I set the user as logged in?

I am testing this in my pages doing :

protected void Page_Load(object sender, EventArgs e)
{
    if ( Membership.GetUser()==null)
    {
        Response.Redirect("/admin/login.aspx");
    }
    // else "you are logged in, congratulations"                
}

I would have used the default functions, but it is just not working and a google search made me think that I will save time by actually recoding all that myself.

Anything will help!

EDIT: Regarding the accepted answer, it is the correct one for "how to set the user as logged in" and works fine. It didn't fixed my specific problem but only a part of it. Thought if you look thought the comments you will find interesting pointers.

EDIT 2 and solution: Ok I finally worked it out thanks to all the comments. Here is what I did, it's simpler than what I expected :

Page that checks login state:

 protected void Page_Load(object sender, EventArgs e)
 {
     if ( !Request.IsAuthenticated)
     {
         Response.Redirect("/admin/login.aspx");
     }  

Log out:

   protected void LoginStatus1_Logout(object sender, LoginCancelEventArgs e)
   {
       FormsAuthentication.SignOut();
       Response.Redirect("/admin/login.aspx");
   }
}

web.config:

<authentication mode="Forms" />

login:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    if(Membership.ValidateUser(Login1.UserName, Login1.Password))
    {
        FormsAuthentication.SetAuthCookie(Login1.UserName, true);
        Response.Redirect("/admin/default.aspx");

    }
}

解决方案

Put this in Login1_Authenticate before calling Response.Redirect("/admin/default.aspx");

FormsAuthentication.SetAuthCookie("username", true);

这篇关于ASP.NET成员资格:如何设置用户的登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆