Tag: asp.net mvc 5

Owin的NullReferenceException

我刚刚将一个项目克隆到一台新机器上,并且我在使用OWIN的MVC站点上获得了一个难以调试的NullReferenceException : [NullReferenceException: Object reference not set to an instance of an object.] Microsoft.Owin.Security.Cookies.d__0.MoveNext() +664 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 Microsoft.Owin.Security.Infrastructure.d__2.MoveNext() +860 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 Microsoft.Owin.Security.Infrastructure.d__0.MoveNext() +427 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +22 Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +33 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +150 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +42 System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 它发生在之后 [assembly: OwinStartupAttribute(typeof(Website.Startup))] […]

User.Identity.GetUserId是否已缓存或每次从数据库中获取?

我正在使用ASP.Net MVC 5,我在我的代码中调用了这一行 string userId = User.Identity.GetUserId(); ASP.NET MVC是否会从表中为每次调用获取此内容,还是会缓存?

如何删除使用UserManager.CreateAsync创建的用户

使用asp.net mvc5,我的用户管理系统似乎工作。 我可以使用谷歌或名称/密码登录.. 但现在我正在开发一个用户管理界面,我需要能够删除现有用户。 这开始向我展示用户管理系统是多么令人困惑。 有很多不同的方式来处理用户..而且其中一些不起作用。 我阅读的大多数地方,都在谈论使用Membership.DeleteUser()。 但这不起作用…… 用户创建了。 var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email, ConfirmationToken = confirmationToken, IsConfirmed = false }; var result = await UserManager.CreateAsync(user, model.Password); 现在稍后..如何删除这样的用户? (给出其名称或用户ID) 我已经尝试过在各种搜索中出现最多的东西..想出了会员资格作为解决方案。 但这肯定不适合MVC5? 例如 var allusers = Membership.GetAllUsers(); // allusers is empty bool success = Membership.DeleteUser(model.name); // <– success = […]

在ASP.NET MVC应用程序中使用HandleErrorAttribute

我有一个关于在我的MVC 5应用程序中使用HandleErrorAttribute的最佳方法的问题。 我们知道,我们可以将此属性添加到全局filter中: filters.Add(new HandleErrorAttribute{View = “Error”}); 这涉及应用程序每次在任何级别的应用程序中抛出未处理的exception时显示“错误”视图。 但是,如果我在其他全局授权或动作filter中有一些逻辑,那产生一些exception,比第一次出现exception时,那么app尝试重定向到Error View,再次其他filter开始执行并再次产生相同的exception,所以asp .net以避免此循环终止应用程序。 那么如何使用这个HandleErrorAttribute来避免这种行为的最佳方法呢? 谢谢! 编辑:经过一些调试我发现这不是HandleErrorAttribute的常见行为,所以只有当我使用自定义路由fe时才会出现这种循环 {key}/{controller}/{action} 当filter逻辑中出现一些错误时,app尝试重定向到错误视图,但是另一个filter逻辑再次开始执行,我甚至在{key}路由参数中看到“错误”值,因此这是不需要的行为。 当我使用默认路由{controller}/{action}这不会发生,并且我完全得到错误视图而没有第二次执行任何全局filter逻辑。

ASP.NET Identity 2 UserManager使所有用户都异步

有人可以告诉你是否有办法让所有用户在ASP.NET Identity 2中异步? 在UserManager.Users中没有任何异步或找到所有异步或类似的somwething

UserManager错误 – 在上一个异步操作完成之前,在此上下文中启动了第二个操作

我使用Identity v2.0.0.0,EF 6,Castle Windsor IOC Container,Microsoft SQL Server 2005,使用我的asp.net MVC5 Web应用程序来解决这个问题 我试图通过使用UserManagerExtensions,FindById()方法获取当前登录用户,但它抛出一个错误“System.NotSupportedException:在上一个异步操作完成之前在此上下文上启动了第二个操作。使用’await’确保在调用此上下文中的另一个方法之前已完成任何异步操作。不保证所有实例成员都是线程安全的“ 我从堆栈跟踪中理解的是“AsyncHelper.RunSync()”导致问题,我没有在我的代码中使用任何异步函数,而是使用AsyncHelper参考的“FindById()” – : http://www.symbolsource。组织/ MyGet /元/ aspnetwebstacknightly /项目/ Microsoft.AspNet.Identity.Core / 2.0.0-beta1-140129 /发行/默认/ Microsoft.AspNet.Identity.Core / Microsoft.AspNet.Identity.Core /扩展/ UserManagerExtensions。 CS?ImageName = Microsoft.AspNet.Identity.Core 关于此Identity错误的信息似乎很少,我在应用程序的不同部分使用当前登录用户的详细信息,当我的服务层调用getCurrentLoggedInUser()函数时,这个错误似乎是随机发生的。 请帮忙…. 我的代码和堆栈跟踪—-注意:我发现了另一个类似类型的事件: https : //aspnetidentity.codeplex.com/workitem/2408 Global.asax中: .Register(Component .For<UserManager>() .UsingFactoryMethod((kernel, creationContext) => new TempoUserManager(new UserStore(new ApplicationDbContext(ASPUserDatabaseConnectionString)))) .LifestylePerWebRequest() ) .Register(Component .For<RoleManager>() .UsingFactoryMethod((kernel, creationContext) => […]

“未指定SMTP主机。” – 但是已指定?

我在这里有点困惑 – 我收到以下错误: 未指定SMTP主机。 即使我的代码看起来是正确的(从我可以看到)。 我可以通过包含控制器内部的所有细节来手动完成,例如 SmtpClient smtpClient = new SmtpClient(“smtp.gmail.com”); smtpClient.Port = 587; … etc 但我不应该这样做,因为我想使用mailSettings的细节(让它可以重复用于各种不同的控制器)。 我的Web.Config文件中的mailSettings : 我的控制器动作: [HttpPost] public ActionResult SubmitFeature(FormData formData) { SmtpClient smtpClient = new SmtpClient(); MailMessage mail = new MailMessage(); mail.To.Add(new MailAddress(“example@gmail.com”)); mail.Body = “Test”; smtpClient.Send(mail); return View(“Example”); } 有什么我想念的可能导致这种情况吗? 我没有搞乱Web.Config中的任何其他设置,它们就像设置新的MVC5项目时一样。

如果返回格式是xml,如何删除web api中的模式节点?

我有一个web api方法,它将format作为一个参数,提供返回xml和json。方法返回的数据类型是DataTable.In json格式一切看起来很好但是xml格式的数据表的模式和xml节点中的一些其他属性还返回。如何返回仅包含datatable数据的简单xml?另外,我在WebApiConfig中使用QueryStringMapping。 这是WebApiConfig代码 public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{action}/{id}”, defaults: new { id = RouteParameter.Optional } ); config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping(“format”, “json”, new MediaTypeHeaderValue(“application/json”))); config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping(“format”, “xml”, new MediaTypeHeaderValue(“application/xml”))); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; } 这是控制器方法的伪代码 [BasicAuthentication] [Route(“api/{tablename}”)] [HttpGet] public IHttpActionResult Get(string tablename, string orders = “”, int limit = 100) { […]

FormsAuthentication对象已废弃

我在MVC5站点中使用以下代码: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel loginModel) { if (ModelState.IsValid) { var authenticated = FormsAuthentication.Authenticate(loginModel.UserName, loginModel.Password); if (authenticated) { FormsAuthentication.SetAuthCookie(loginModel.UserName, true); return RedirectToAction(“AdminPanel”); } ModelState.AddModelError(“”, “The username and password combination were incorrect”); } return View(loginModel); } 这引发了以下警告: System.Web.Security.FormsAuthentication.Authenticate(string,string)’已过时:’建议的替代方法是使用Membership API,例如Membership.ValidateUser。 有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=252463 。 首先是免责声明 – 我是那些喜欢与事物保持同步的开发人员之一,我更愿意完全避免VS中的警告。 但是,在这个特定的实例中,我使用的是非常原始的身份validation,它直接来自web.config,如下所示: 在这个项目中绝对没有进一步的登录要求 – 使用数据库是过度的,并且不需要分布式登录; 基本上,将详细信息存储在web.config中是最理想的解决方案,每个应用程序可能只有一个用户。 我无疑会从控制器(使用DI / IoC)中抽象出认证代码,但我仍然打算使用FormsAuthentication对象对web.config中的详细信息进行身份validation。 虽然我完全了​​解会员提供商,DotNetOpenAuth和新的(但非常可怕的)基于OWIN的身份validation模型,但上述代码绰绰有余。 第一个问题 – […]

SelectedValues在MultiSelectList mvc中不起作用

我有一个类似的课程 public class Category { public int ID { get; set; } public string Name { get; set; } public ICollection CategorySelected { get; set; } public static List GetOptions() { var categories = new List(); categories.Add(new Category() { ID = 1, Name = “Bikes” }); categories.Add(new Category() { ID = 2, Name = “Cars” […]