如何使用ASP在数据库中保存数据.. NET身份(Web表单)

本文关键字:身份 NET Web 表单 数据 保存 ASP 何使用 数据库 | 更新日期: 2023-09-27 18:07:33

这个问题是下一章:如何在ASP中添加自定义表。净身份?

在第一章中,我介绍了如何在ASP中创建自定义表。净的身份。在本章中,我将讨论如何在数据库中保存数据。我试了很多方法,但都没有成功。

登录成功后,系统应存储如下数据:

  • [AccountLogID] PK
  • (IPv4)
  • LoginDate
  • [UserId] FK (From User.ID)

我的代码是这样的:

IdentityModels.cs

namespace Web_WebApp.Models
{
    // You can add User data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {

        public virtual ICollection<AccountLog> AccountLogs { get; set; }

        public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
        public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            return Task.FromResult(GenerateUserIdentity(manager));
        }
    }

    public class AccountLog
    {
        [Key]
        public Guid AccountLogID { get; set; }
        public string IPv4 { get; set; }
        public DateTime LoginDate { get; set; }
        public string UserId { get; set; }
        [ForeignKey("UserId")]
        public virtual ApplicationUser User { get; set; }
    }
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    {
        public ApplicationDbContext()
            : base("Web_Identity", throwIfV1Schema: false)
        {
        }

        public System.Data.Entity.DbSet<AccountLog> AccountLog { get; set; }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

    }
}

Login.aspx.cs

namespace Web_WebApp.Account
{
    public partial class Login : Page
    {
        string GuidToken = System.Guid.NewGuid().ToString();

        public static string GetExternalIP()
        {
            try
            {
                string externalIP;
                externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
                externalIP = (new Regex(@"'d{1,3}'.'d{1,3}'.'d{1,3}'.'d{1,3}"))
                             .Matches(externalIP)[0].ToString();
                return externalIP;
            }
            catch { return null; }
        }

        protected void LogIn(object sender, EventArgs e)
        {
            // Validate the user password
            //var AccountLogs = Context.GetOwinContext().GetUserManager<AccountLog>();
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
            // Require the user to have a confirmed email before they can log on.
            var user = manager.FindByName(username.Text);
            if (IsValid)
            {

                if (user != null)
                {
                    {
                        // This doen't count login failures towards account lockout
                        // To enable password failures to trigger lockout, change to shouldLockout: true
                        var result = signinManager.PasswordSignIn(username.Text, Password.Text, RememberMe.Checked, shouldLockout: true);
                        if (!user.EmailConfirmed && result == SignInStatus.Success)
                        {

                            Response.Redirect("/Account/Confirmation?UserConfirmationID=" + user.Id);
                        }

                        switch (result)
                        {
                            case SignInStatus.Success:
                                var AccountLog = new AccountLog()
                                {
                                    IPv4 = GetExternalIP(),
                                    LoginDate = DateTime.Now,
                                    UserId = user.Id,
                                };
                                user.AccountLogs.Add(AccountLog);

                                IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                                break;
                            case SignInStatus.LockedOut:
                                //Response.Redirect("/Account/Lockout");
                                FailureText.Text = "This account has been locked, please try again later.";
                                ErrorMessage.Visible = true;
                                return;
                            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 password";
                                ErrorMessage.Visible = true;
                                break;
                        }

                    }

                }
                else
                    FailureText.Text = "Account not found.";
                ErrorMessage.Visible = true;
            }
        }
    }
}
使用这些代码,我的应用程序运行,我可以登录,但它不保存任何东西,正如我在我的代码中提到的。我没有得到任何错误或警告。

我确定像"SaveChanges()"这样的东西丢失了,但我不知道把它放在哪里。

我很感激你为解决我的问题所做的努力。

如何使用ASP在数据库中保存数据.. NET身份(Web表单)

您通过ApplicationUserManager获取用户,在向user.AccountLogs集合添加新的AccountLog条目后,您需要将用户保存回数据库。我敢说,你可以从OWIN获得实体框架上下文(ApplicationDbContext ?),就像你获得其他实例一样。

switch (result)
{
    case SignInStatus.Success:
        var AccountLog = new AccountLog()
        {
            IPv4 = GetExternalIP(),
            LoginDate = DateTime.Now,
            UserId = user.Id,
        };
        user.AccountLogs.Add(AccountLog);
        // get the entity framework context.
        var dbContext = Context.GetOwinContext().Get<ApplicationDbContext>();
        // save the changes to objects tracked by this context
        dbContext.SaveChanges();

        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
        break;
case SignInStatus.LockedOut:
 // removed for brevity.
    break;
}

注意:您可能必须从上下文中直接获得用户,但我认为这不是必要的,这取决于用户是否分离