简体   繁体   English

如何在ASP.Net Web应用程序模板中更改登录功能?

[英]How to change the login function in ASP.Net web application template?

I am working on a project that is using the web application template in VS 2015. I want to change the login function so that the authentication process will use the private database, but I have had a hard time understanding the code. 我正在处理一个在VS 2015中使用Web应用程序模板的项目。我想更改登录功能,以便身份验证过程将使用私有数据库,但是我很难理解代码。

The following is the origin code (I have coded to use the private database for the login function, but it failed to keep the login status): 以下是原始代码 (我已经编码为使用私有数据库来登录功能,但是无法保持登录状态):

using System;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using WebApplication2.Models;

namespace WebApplication2.Account
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register";
            // Enable this once you have account confirmation enabled for password reset functionality
            //ForgotPasswordHyperLink.NavigateUrl = "Forgot";
            OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }
        }

        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                // Validate the user password
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();

                // This doen't count login failures towards account lockout
                // To enable password failures to trigger lockout, change to shouldLockout: true
                var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);

                switch (result)
                {
                    case SignInStatus.Success:
                        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                        break;
                    case SignInStatus.LockedOut:
                        Response.Redirect("/Account/Lockout");
                        break;
                    case SignInStatus.RequiresVerification:
                        Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", 
                                                        Request.QueryString["ReturnUrl"],
                                                        RememberMe.Checked),
                                          true);
                        break;
                    case SignInStatus.Failure:
                    default:
                        FailureText.Text = "Invalid login attempt";
                        ErrorMessage.Visible = true;
                        break;
                }
            }
        }
    }
}

The UserManager should have an underlying DbContext associated with it. UserManager应该具有与其关联的基础DbContext。 You can change the connection string name associated with that context. 您可以更改与该上下文关联的连接字符串名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在空的 ASP.NET web 应用程序模板中与后端通信? - How to communicate with backend in an empty ASP.NET web application template? “ ASP.NET Web应用程序”模板不存在 - 'ASP.NET Web Application' template not there 在ASP.NET Web应用程序中使用功能 - Using function in ASP.NET web application 缺少“ASP.NET Core Web 应用程序(.NET Core)”模板 - missing “ASP.NET Core Web Application (.NET Core)” template ASP.NET / C#Web应用程序上的登录区域 - Login area on an ASP.NET / C# web application VS 2013-ASP.NET Web应用程序模板 - VS 2013 - ASP.NET Web Application Template 如何在ASP.NET MVC Web应用程序中将登录页面设置为启动页面? - How to make login page a startup page in ASP.NET MVC web application? 如何使用 Angular 个人用户帐户身份验证自定义 ASP.NET Core Web 应用程序的登录页面? - How to customize the login page of ASP.NET Core web application with Angular Individual user account authentication? 如何使用自定义登录服务器验证 asp.net web 应用程序? - How can I authenticate asp.net web application with custom login server? 使用asp.net C#Javascript使用Finerprint登录到Asp.net Web应用程序 - Login into Asp.net Web application with Finerprint using asp.net C# Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM