新的ASP.net Identity项目为网站安全带来了一些有用的代码和接口.要使用接口实现自定义系统(而不是使用MVC 5模板中包含的标准Entity Framework实现),IPasswordHasher
则需要使用.
IPasswordHasher
ASP.net身份中的接口namespace Microsoft.AspNet.Identity
{
public interface IPasswordHasher
{
string HashPassword(string password);
PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword);
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用密码salting在ASP.net身份中通过此接口进行更安全的加密?
我看到我维护的一些代码存在问题.下面的代码有一个private static SHA1
成员(这是一个IDisposable
但是因为它static
,它应该永远不会最终确定).但是,在压力下,此代码会抛出一个异常,表明它已被关闭:
Caught exception. Safe handle has been closed"
Stack trace: Call stack where exception was thrown
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 cbData, Int32 ibStart, Int32 cbSize)
at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 ibStart, Int32 cbSize)
at System.Security.Cryptography.HashAlgorithm.ComputeHash(Byte[] buffer)
Run Code Online (Sandbox Code Playgroud)
有问题的代码是:
internal class TokenCache
{
private static SHA1 _sha1 = SHA1.Create();
private string ComputeHash(string password)
{
byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
return UTF8Encoding.UTF8.GetString(_sha1.ComputeHash(passwordBytes));
}
Run Code Online (Sandbox Code Playgroud)
我的问题显然是可能导致这个问题的原因.呼叫是否可以SHA1.Create
静默失败(有多少加密资源可用)?这可能是因为appdomain失败了吗?
还有其他理论吗?
我有一个简单的猜数游戏.它具有询问您是否需要提示的功能.它将响应保存在boolean
调用中tips
,如图所示.
while (run) {
while (tinvalidrun){
System.out.println("Do you want any tips? y or n?");
input=in.next();
switch(input){
case "y":
System.out.println("Ok, we will tell you how close you are!");
tinvalidrun=false;
tips=true;
break;
case "n":
System.out.println("Wanna go hard eh? Well, go along!");
tinvalidrun=false;
break;
default:
System.out.println("You pressed the wrong key!\nDo you have butter fingers?");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一些代码可以生成更多变量并生成一个随机数.最后我得到猜测的用户输入并测试它:
do {
System.out.println("Enter a number: ");
guess = in.nextByte();
times++;
if (guess == gval) {
break;
} else {
System.out.println("No luck! Try …
Run Code Online (Sandbox Code Playgroud) 我有一些标有的辅助方法[Conditional("XXX")]
.目的是在仅存在XXX条件编译符号时使方法有条件地编译.我们使用它来调试和跟踪功能,它运行良好.
在我研究条件编译如何工作的过程中,我发现有几个来源说明用Conditional
属性标记的方法将放在IL中,但是不会执行对方法的调用.
代码如何编译成IL而不执行?如何验证行为实际上是如上所述?我没有对IL做过多少工作,所以我的技能在这个方面都很弱.
为什么可以插入String
到List<Integer>
在下面的代码?我有一个类将数字插入到整数列表中:
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(3);
list.add(4);
Inserter inserter = new Inserter();
inserter.insertValue(list);
System.out.print(list);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我有一个单独的类,其插入String
到一个List
,与所述数字串值"42"
:
public class Inserter {
void insertValue(List list)
{
list.add(new String("42"));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不会引发编译器错误,或者运行时抛出运行时异常,例如*CastException
,当我添加String
到整数列表时?另外,为什么System.out.print(list)
产生如下的输出而不抛出任何异常?
[2, 3, 4, 42]
Run Code Online (Sandbox Code Playgroud)
允许所有这些发生的根本原因是什么?
我从< p:editor>
这里得到一个String : < b>This is bold text< /b>
. 我想<b>This is bold text</b>
在xhtml页面中显示.我可以使用什么标签来做到这一点?
我们的应用生成图像.消耗的内存会BufferedImage
产生内存不足异常:
java.lang.OutOfMemoryError:Java堆空间
这发生在以下行:
BufferedImage result = new BufferedImage(2540, 2028, BufferedImage.TYPE_INT_ARGB);
Run Code Online (Sandbox Code Playgroud)
在此指令之前检查空闲内存时,它显示我有108MB可用内存.我用来检查内存的方法是:
Runtime rt = Runtime.getRuntime();
rt.gc();
long maxMemory = rt.maxMemory();
long usedMemory = rt.totalMemory() - rt.freeMemory();
long freeMem = maxMemory - usedMemory;
Run Code Online (Sandbox Code Playgroud)
我们不明白它如何BufferedImage
消耗超过100MB的内存.它应该使用2540*2028*4字节,大约20 MB.
创建时为什么会消耗这么多内存BufferedImage
?我们可以做些什么来减少这种情况?
我正在通过一系列解释和练习来学习Java,其中之一是创建一个程序,该程序将根据许多点(0–29、30–34、35)显示数字等级(0-5) –39、40–44、45–49、50-60)。
System.out.println("Type the points [0-60]: ");
double points = reader.nextDouble();
reader.nextLine();
if (points < 29) {
System.out.println("Grade: FAILED.");
} else if (points <= 34) {
System.out.println("Grade: 1.");
} else if (points <= 39) {
System.out.println("Grade: 2.");
} else if (points <= 44) {
System.out.println("Grade: 3.");
} else if (points <= 49) {
System.out.println("Grade: 4.");
} else if (points >= 50) {
System.out.println("Grade: 5.");
}
Run Code Online (Sandbox Code Playgroud)
该程序的工作方式是由于命令重叠而给出正确的等级,但是有什么方法可以创建一系列满足if / else语句条件的数字或字符串?例如,如果输入的数字在40-44之间,依此类推。由于我是新来的,所以详细的答案将不胜感激。
我必须通过Entity Framework 5使用以下模型创建数据库:
public class Post
{
public int PostId { get; set; }
[MaxLength(200)]
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我添加了新的属性 Post
public string Abstract { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后我跑了
Add-Migration AddPostAbstract
Run Code Online (Sandbox Code Playgroud)
在我的Migrations
文件夹中创建了以下类,之后我通过添加一个SQL语句修改了这个文件
//201308300714477_AddPostAbstract.cs
public override void Up()
{
AddColumn("dbo.Posts", "Abstract", c => c.String());
Sql("UPDATE dbo.Posts SET Abstract = LEFT(Content, 100) WHERE Abstract IS …
Run Code Online (Sandbox Code Playgroud) 假设我的nlog.config中有以下内容(取自http://nlog-project.org/documentation/v2.0.1/html/T_NLog_Targets_MemoryTarget.htm):
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="memory" xsi:type="Memory" layout="${message}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="memory" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
如何以编程方式访问此目标?我试图在文本框中显示日志.