簡體   English   中英

使用 ASP.NET 中的會員 API 登錄表單

[英]login form using Membership API in ASP.NET

我是新來的ASP.NET,

我想使用會員資格 API 創建登錄表單,我無法使用我的憑據登錄,

這是我的代碼片段

LoginForm.aspx.cs

protected void Submit_Click(object sender, EventArgs e)
{
    if (FormsAuthentication.Authenticate(Username.Text, Password.Text))
    {
        Response.Write("Welcome " + Username.Text);
    }
    else
    {
        Response.Write("Sorry Login Failed ");
    }
}

我使用會員資格創建了注冊表 API

這是我的RegistrationForm.aspx.cs代碼片段

 protected void AddUser_Click(object sender, EventArgs e)
{
    MembershipCreateStatus result;
    try
    {
        MembershipUser newUser = Membership.CreateUser(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, true, out result);
        if (result == MembershipCreateStatus.Success)
        {
            Response.Write("Successfulley created");
        }
        else
        {
            Response.Write("Fail to Register");
        }
    }
    catch (Exception err)
    {
        Response.Write(err.Message);
    }
}

當我在 RegistrationForm 中注冊新用戶時,我得到Successfulley created的 output,但是在注冊后,當我使用注冊的用戶名和密碼登錄時,我得到 output,因為Sorry Login Failed

你需要用這個嗎?

FormsAuthentication.SetAuthCookie

@dhaval 試試這個,

protected void SubmitClick(object sender, EventArgs e)
{
    try
    {
        if (Membership.ValidateUser(Username.Text, Password.Text))
        {
            FormsAuthentication.SetAuthCookie(Username.Text, true);
            FormsAuthentication.RedirectFromLoginPage(Username.Text, true);

            Response.Redirect("NextPage.aspx");
        }
        else
        {
            Response.Write("Sorry Login Failed ");
        }
    }
    catch (Exception err)
    {
        Response.Write(err.Message);
    }
}

Go 從 VS 解決方案資源管理器的右上角轉到website administration tool ,您將看到如下截圖所示的內容,如果您可以看到已創建的用戶,您旁邊會有一個復選框,如果它被選中,您可以登錄,否則將其選中然后你就可以成功登錄了。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM