发布到azure后关于MVC4 asp.net登录应用程序的错误

本文关键字:net asp 登录 应用程序 错误 MVC4 azure | 更新日期: 2023-09-27 18:11:44

我按照视频构建了一个小的登录应用程序

https://www.youtube.com/watch?v=7RoJIgRcuOc&列表= PLt2cGgt6G8Wo8CoYLGX31mbHU4L-Erpkg

我首先创建了数据库,然后使用该数据库创建实体框架,使我从数据库中获取内容。然后我在密码验证过程中添加了一个小哈希。下面是我的用户控制器的代码:

namespace LogInSystem.Controllers
{
    public class UserController : Controller
    {
        public const int SALT_BYTE_SIZE = 24;
        public const int HASH_BYTE_SIZE = 24;
        public const int PBKDF2_ITERATIONS = 1000;
        //
        // GET: /User/
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult LogIn()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LogIn(Models.UserModel user)
        {
            if (ModelState.IsValid)
            {
                if (IsValid(user.Email, user.Password))
                {
                    FormsAuthentication.SetAuthCookie(user.Email, false);
                    return RedirectToAction("Index", "User");
                }
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect");
                }
            }
            return View(user); 
        }
        [HttpGet]
        public ActionResult Registration()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Registration(Models.UserModel user)
        {
            if(ModelState.IsValid)
            {
                using(var db = new MainDbEntities())
                {
                    var sysUser = db.SystemUsers.Create();
                    // create salt and password hash
                    RNGCryptoServiceProvider csprng = new RNGCryptoServiceProvider();
                    byte[] salt = new byte[SALT_BYTE_SIZE];
                    csprng.GetBytes(salt);
                    byte[] hash = PBKDF2(user.Password, salt, PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
                    sysUser.Email = user.Email;
                    sysUser.Password = Convert.ToBase64String(hash);
                    sysUser.PasswordSalt = Convert.ToBase64String(salt);
                    sysUser.UserId = Guid.NewGuid();
                    db.SystemUsers.Add(sysUser);
                    db.SaveChanges();
                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
        private bool IsValid (string email, string password)
        {
            bool isValid = false;
            using (var db = new MainDbEntities())
            {
                var user = db.SystemUsers.FirstOrDefault(a => a.Email == email);
                if(user != null)
                {
                    byte[] salt = Convert.FromBase64String(user.PasswordSalt);
                    byte[] correctHash = Convert.FromBase64String(user.Password);
                    byte[] testHash = PBKDF2(password, salt, PBKDF2_ITERATIONS, correctHash.Length);
                    if(SlowEquals(correctHash, testHash))
                    {
                        isValid = true;
                    }
                }
            }
            return isValid;
        }
        // compute the hash of a password using PBKDF2-SHA1
        private byte[] PBKDF2(string password, byte[] salt, int iterations, int outputBytes) 
        {
            Rfc2898DeriveBytes pbkdf2 = new Rfc2898DeriveBytes(password, salt);
            pbkdf2.IterationCount = iterations;
            return pbkdf2.GetBytes(outputBytes);
        }
        // Compare two byte arrays
        private bool SlowEquals(byte[] a, byte [] b) 
        {
            uint diff = (uint)a.Length ^ (uint)b.Length;
            for(int i = 0; i < a.Length && i < b.Length; i++) 
            {
                diff |= (uint)(a[i] ^ b[i]);
            }
            return diff == 0;
        }
    }
}

之后,我想将应用程序发布到azure。我检查了很多方法。我试着从蔚蓝的一面进行创作。我创建了网站和服务器。然后我在服务器上创建了一个数据库。我将连接字符串复制到web.config。我也试过在Visual Studio中发布,但还是不行。这是我的web.config

中的连接字符串
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)'v11.0;Initial Catalog=aspnet-LogInSystem-20140626152022;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|'aspnet-LogInSystem-20140626152022.mdf" />
<add name="MainDbEntities" connectionString="metadata=res://*/MainDbModel.csdl|res://*/MainDbModel.ssdl|res://*/MainDbModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)'v11.0;attachdbfilename=|DataDirectory|'MainDb.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

当我每次尝试注册时。它会得到共享误差。我很困惑,不知道如何解决这个问题。

这是上下文。

public partial class MainDbEntities : DbContext
{
    public MainDbEntities()
        : base("name=MainDbEntities")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
    public DbSet<SystemUsers> SystemUsers { get; set; }
}

发布到azure后关于MVC4 asp.net登录应用程序的错误

现在你的连接字符串是针对本地数据库,修改它为azure,如

<connectionStrings>
  <add name="DefaultConnection" connectionString="Server=SERVER_NAME;Database=DATABASE_NAME;User ID=USER_ID;Password=PASSWORD;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
</connectionStrings>

设置连接字符串后,查看dbContext,检查它是否指向正确的连接字符串。你的dbContext应该像

public class YourContext : DbContext
{
    public YourContext() : base("DefaultConnection"){ }
}