Tag: .net

ASP.NET使用SqlConnection连接MySQL

这是web.config保存的连接字符串: 这是连接数据库的代码: protected bool CheckPasswordBySqlServer(string strEmail, string strPsw) { if (strEmail.ToLower() == “admin”) { return false; } string str = “select id,Rank,RankEnc,ParentUser,Company from tbl_User where userName=@UserName and password1=@password”; private string strConn = ConfigurationManager.AppSettings[“conn”].ToString(); SqlConnection sqlConnection = new SqlConnection(strConn); bool flag = false; try { try { sqlConnection.Open(); SqlCommand sqlCommand = new SqlCommand(str, sqlConnection); sqlCommand.Parameters.AddWithValue(“UserName”, strEmail); […]

“填充无效,无法删除”使用AesManaged

我正在尝试使用AesManaged进行简单的加密/解密,但在尝试关闭解密流时我一直遇到exception。 这里的字符串被正确加密和解​​密,然后在Console.WriteLine输出正确的字符串后,我得到CryptographicException“Padding无效且无法删除”。 有任何想法吗? MemoryStream ms = new MemoryStream(); byte[] rawPlaintext = Encoding.Unicode.GetBytes(“This is annoying!”); using (Aes aes = new AesManaged()) { aes.Padding = PaddingMode.PKCS7; aes.Key = new byte[128/8]; aes.IV = new byte[128/8]; using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(rawPlaintext, 0, rawPlaintext.Length); cs.FlushFinalBlock(); } ms = new MemoryStream(ms.GetBuffer()); using (CryptoStream cs = new […]

DataGridView keydown事件无法在C#中工作

当我在单元格内编辑文本时,DataGridView keydown事件不起作用。 我正在分配快捷键Alt + S来保存数据,它在单元格不处于编辑模式时起作用,但如果它处于编辑模式下面,则代码不起作用 private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == (Keys.Alt | Keys.S)) { //save data } }

将Perl正则表达式转换为.NET

我在Perl中有一些有用的正则表达式 。 有没有一种简单的方法将它们转换为.NET的正则表达式方言? 如果没有,是否有简明的差异参考?

无法将JSON数组反序列化为类型–Json.NET

我试图将json数据反序列化为模型类,但我失败了。 这是我做的: public CountryModel GetCountries() { using (WebClient client = new WebClient()) { var result = client.DownloadString(“http://api.worldbank.org/incomeLevels/LIC/countries?format=json”); var output = JsonConvert.DeserializeObject<List>(result); return output.First(); } } 这就是我的模型的样子: public class CountryModel { public int Page { get; set; } public int Pages { get; set; } public int Per_Page { get; set; } public int Total { […]

授权标头在重定向时丢失

下面是进行身份validation的代码,生成Authorization标头并调用API。 不幸的是,我在API上发出GET请求后收到401 Unauthorized错误。 但是,当我捕获Fiddler中的流量并重放它时,对API的调用成功,我可以看到所需的200 OK状态代码。 [Test] public void RedirectTest() { HttpResponseMessage response; var client = new HttpClient(); using (var authString = new StringContent(@”{username: “”theUser””, password: “”password””}”, Encoding.UTF8, “application/json”)) { response = client.PostAsync(“http://host/api/authenticate”, authString).Result; } string result = response.Content.ReadAsStringAsync().Result; var authorization = JsonConvert.DeserializeObject(result); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorization.Scheme, authorization.Token); client.DefaultRequestHeaders.Add(“Accept”, “application/vnd.host+json;version=1”); response = client.GetAsync(“http://host/api/getSomething”).Result; Assert.True(response.StatusCode == HttpStatusCode.OK); […]

暂停/恢复异步任务的模式?

我有一个主要是IO绑定的连续任务(后台拼写检查器与拼写检查服务器通信)。 有时,此任务需要暂停并稍后恢复,具体取决于用户活动。 虽然暂停/恢复基本上是async/await所做的,但我发现很少有关于如何为异步方法实现实际暂停/播放逻辑的信息。 有推荐的模式吗? 我也考虑过使用Stephen Toub的AsyncManualResetEvent ,但认为这可能是一种矫枉过正。

无法更新.mdf数据库,因为数据库是只读的(Windows应用程序)

我在C#中创建了一个数据库窗口应用程序。 我的应用程序在Windows XP上成功运行,但在Vista或Windows 7系统上无法正常执行。 我的应用程序显示类似的消息 无法更新.mdf数据库,因为数据库是只读的 任何人都可以给我一个解决方案吗?

.NET中的密码加密/解密代码

我希望在C#中简单加密和解密密码。 如何在数据库中以加密格式保存密码,并通过解密检索原始格式?

在TextBox C#中更改边框颜色

我有以下代码: public class OurTextBox : TextBox { public OurTextBox() : base() { this.SetStyle(ControlStyles.UserPaint, true); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Pen penBorder = new Pen(Color.Gray, 1); Rectangle rectBorder = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width – 1, e.ClipRectangle.Height – 1); e.Graphics.DrawRectangle(penBorder, rectBorder); } } 这是完美的,但它没有显示文本,直到它获得焦点。 有谁能够帮我? 怎么了? 预先感谢。