Tag: asp.net mvc

Asp.net MVC – 如何哈希密码

如何将用户输入(密码)哈希到数据库,然后在登录期间读取哈希密码? 我相信解决方案是在注册时散列密码,其中密码在db中保存为散列。 登录后,它应该取消哈希并将其密码与用户密码输入进行比较。 但我不知道该怎么做。 我允许密码在db中具有nvarchar(MAX) ,因为散列密码通常很长。 [Required] [StringLength(MAX, MinimumLength = 3, ErrorMessage = “min 3, max 50 letters”)] public string Password { get; set; } 寄存器: [HttpPost] public ActionResult Register(User user) { if (ModelState.IsValid) { var u = new User { UserName = user.UserName, Password = user.Password }; db.Users.Add(u); db.SaveChanges(); return RedirectToAction(“Login”); } }return View(); […]

web api中的内存缓存

我在我的web api中寻找缓存 ,我可以使用一个api方法的输出(在12小时内更改一次)进行后续调用,然后我在SO上找到了这个解决方案 ,但是我很难理解并使用下面的代码 private IEnumerable GetFromCache(string key, Func<IEnumerable> valueFactory) where TEntity : class { ObjectCache cache = MemoryCache.Default; var newValue = new Lazy<IEnumerable>(valueFactory); CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30) }; //The line below returns existing item or adds the new value if it doesn’t exist var value = cache.AddOrGetExisting(key, newValue, policy) […]

使用Owin的Twitter外部登录提供HTTP 403(禁止)

ASP.NET MVC 5项目。 Owin版本4.0.0.0 Startup.Auth app.UseTwitterAuthentication( consumerKey: “somekey”, consumerSecret: “someSecretKey”); ExternalLogin方法中发生exception 例外:响应状态代码不表示成功:403(禁止)。 以下是回调url 仅供参考:网站是真实的,并且工作没有问题,Twitter登录在几周前工作,但是现在,突然,开始失败,出现上一个错误。 堆栈跟踪 [HttpRequestException: Response status code does not indicate success: 403 (Forbidden).] System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() +121834 Microsoft.Owin.Security.Twitter.d__23.MoveNext() +2387 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 Microsoft.Owin.Security.Twitter.d__12.MoveNext() +1091 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 Microsoft.Owin.Security.Infrastructure.d__b.MoveNext() +376 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 Microsoft.Owin.Security.Infrastructure.d__8.MoveNext() +475 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 […]

ASP.NET MVC + LINQexception

我有一个ASP.NET MVC应用程序,99.9%的时间都很好用。 一旦陷入蓝色的月亮,虽然事情确实出现了问题,我想知道是否有人可以对这里可能出现的问题有所了解。 Web应用程序正在使用Linq2SQL,并在以下一组指令后在控制器中爆炸: const int pageSize = 5; var allHeadings = artRepository.FindAllVisibleHeadings(); var paginatedHeadings = new PaginatedList(allHeadings, id ?? 0, pageSize); allHeadings只包含文章所有可见标题的IQueryable列表,而PaginatedList负责从这个很长的列表中取出一个合适的块。 它如下: public PaginatedList(IQueryable source, int pageIndex, int pageSize) { PageIndex = pageIndex; PageSize = pageSize; TotalCount = source.Count(); TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize); this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize)); } 它会在source.Count()行上爆炸,所以当它计算db中的所有可见文章时。 有趣的是,当我多次重新加载页面时,我得到两种不同的例外: 第一:序列包含多个元素 at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression […]

html输出中的特殊字符

我希望直接打印像’ø’这样的字符,但在源’ø’中显示为ø 如果输出通过Html.Encode() ,它将不会呈现正确的字符,但是ø 在我的配置中 资源: 这输出: Støvletter 但输出应该如下: Støvletter

如何避免在视图中循环?

当我构建我的视图时,我想尽可能多地消除处理逻辑。 理想情况下,我想坚持使用Razor只使用HTML和渲染值。 假设我有一个带有BarModel对象列表的FooModel: public class FooModel { public List Bars { get; set; } public string FoosFirstValue { get; set; } public string FoosOtherValue { get; set; } } public class BarModel { public string SomeValue { get; set; } public string SomeOtherValue { get; set; } } 在我看来,我需要遍历并显示我的Bars上的元素: @model MyApp.FooModel @Model.FoosFirstValue @Model.FoosOtherValue Bars: @foreach […]

OWIN上的Application_PreSendRequestHeaders()

我有一个不使用OWIN中间件的应用程序,并具有以下Global.asax : public class MvcApplication : HttpApplication { protected void Application_Start() { //… } protected void Application_PreSendRequestHeaders() { Response.Headers.Remove(“Server”); } } 每次应用程序发送响应时,都会删除Server标头。 如何使用使用OWIN的应用程序执行相同操作? public class Startup { public void Configuration(IAppBuilder application) { //… } //What method do I need to create here? }

ASP MVC C#:是否可以将动态值传递给属性?

好吧,我是C#的新手,我正在尝试使用ASP MVC2创建一个小网站。 我想创建自己的授权属性。 但如果可能,我需要传递一些值。 例如: [CustomAuthorize(GroupID = Method Parameter?] public ActionResult DoSomething(int GroupID) { return View(“”); } 我想授权访问页面。 但它取决于传递给控制器​​的值。 因此授权取决于groupID。 这有可能以任何方式实现这一目标吗? 提前致谢。

没有会话绑定到当前上下文

我遵循了这个教程: http : //nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx 在尝试加载页面时,我没有得到“没有绑定到当前上下文的会话”错误(mvc 3)。 public static ISessionFactory BuildSessionFactory() { return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 // .ConnectionString(@”Server=.\SQLExpress;Database=db1;Uid=dev;Pwd=123;”) .ShowSql()) //.ExposeConfiguration(c => c.SetProperty(“current_session_context_class”, “web”)) //.CurrentSessionContext() .Mappings(m => m.FluentMappings .AddFromAssemblyOf()) .ExposeConfiguration(cfg => new SchemaExport(cfg) .Create(false, false)) .BuildSessionFactory(); } 实际错误在我的Repository.cs文件中: 第114行:公共虚拟T Get(int id)第115行:{第116行:return _sessionFactory.GetCurrentSession()。Get(id); 第117行:}第118行: 当我调试它时,_sessionFactory不是null或者任何东西,它似乎无法找到绑定的会话。 我在我的web.config中连接了httpmodule,它确实运行,所以这不是问题。 在我的nhibernate配置中,我尝试了两种方法: .ExposeConfiguration(c => c.SetProperty(“current_session_context_class”, “web”)) 和 .CurrentSessionContext() 但那没用。

MVC Bundling – 包含单个bundle的.min文件

我的应用程序中有一个文件,我只有一个缩小版本,我想为它创建一个包: bundles.Add(new ScriptBundle(“~/bundles/maskedinput”).Include( “~/Scripts/jquery.maskedinput-1.3.min.js”)); 问题是默认情况下在调试模式下,捆绑机制会忽略.min文件。 我不想为所有捆绑包关闭此规则,但我想为此单个捆绑包禁用它。 这可能吗?