asp.net-mvc-5

How do I mock controller context in my unit test so that my partial view to string function works?

孤街醉人 提交于 2019-12-03 05:30:25
I am attempting to create a unit test for my controller, but the action I am testing uses a partial view to string function which doesn't want to work in my tests. private string RenderPartialViewToString(string viewName, object model = null) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.RouteData.GetRequiredString("action"); ViewData.Model = model; using (System.IO.StringWriter sw = new System.IO.StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); ViewContext viewContext = new ViewContext(ControllerContext,

MVC: How to get controller to render partial view initiated from the view

余生颓废 提交于 2019-12-03 05:29:06
In my MVC5 project I want to create a menu in a partial view. This menu is dynamic in the sense that it is built from content in my database. Thus I have a controller taking care of creating my menu and returning the menu model to my partial view: public PartialViewResult GetMenu() { MenuStructuredModel menuStructuredModel = menuBusiness.GetStructuredMenu(); return PartialView("~/Views/Shared/MenuPartial", menuStructuredModel); } In my partial view called MenuPartial I want to use razor to iterate over my menu items, like: @model MyApp.Models.Menu.MenuStructuredModel <div class="list-group

CookieAuthenticationOptions, ExpireTimeSpan does not work

早过忘川 提交于 2019-12-03 05:16:43
I have the following code: public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, ExpireTimeSpan = System.TimeSpan.FromMinutes(1), LoginPath = new PathString("/Account/Login"), LogoutPath = new PathString("/Account/LogOff") }); But login session active more than 1 minute. Also, LogoutPath is not called when time is expired. Why? Adas Petrovas It does expire. Make sure you do not have any background ajax activity as it extends the session ( SlidingExpiration is true by default)

Windows Authentication with Active Directory Groups

Deadly 提交于 2019-12-03 05:13:48
问题 I have a small project, built using Visual Studio 2013, .NET 4.5, MVC 5, and EF 6. I created it using Windows authentication, but now I need to check for membership in an Active Directory group to allow or deny access. I've gone down many VERY deep rabbit holes, attempting to find out how to do this. At first I presumed that I would need to change the project to use "On-Premises" authentication. However, I have found that: There apparently isn't a way in VS 2013 to change the type of

Asp.Net MVC 5 without Owin?

不问归期 提交于 2019-12-03 04:54:18
问题 Mvc 5 seems to depend on Owin, which is great if you want to self host or run on a Mac. But lets assume I just want to run under IIS just like the previous versions and I'm not interested in what Owin has to offer. The default "blank" mvc5 template uses owin and 15 other dependencies. I've tried removing packages one by one but it seems like the site didn't know how to start without using an attribute from Owin. So, how do I get just ASP.net, mvc 5, under iis without Owin? 回答1: The easy way

How do I create a ClaimsIdentity object for Asp.NET MVC 5?

不羁的心 提交于 2019-12-03 04:50:15
问题 I am working in a .NET MVC 5 application. I do not want to use Entity Framework. I want to authenticate to a RavenDB database. It looks to me that I want to replace the UserManager that comes with the Account Controller. I think I can rewrite all the UserManager functions to work with my database, except I don't understand the ClaimsIdentity object. In the SignInAsync method, there is a call to UserManager.CreateIdentityAsync(...) . I know it returns a ClaimsIdentity object. What I don't know

ASP.NET Web API generate url using Url.Action

回眸只為那壹抹淺笑 提交于 2019-12-03 04:43:55
问题 How can I generate the same url but in Web Api ? var url = Url.Action("Action", "Controller", new { product = product.Id, price = price }, protocol: Request.Url.Scheme); P.S. The url should be generated to an MVC controller/action but from within web api. So basically: make a get request to my api/generateurl and that will return an url to : http://domain.com/controller/action?product=productId&price=100 回答1: Maybe the closest helper to Url.Action in Web Api Controller is the Url.Link method

Setting up Forms Authentication for multiple Web Apps in MVC 5 based on OWIN

那年仲夏 提交于 2019-12-03 04:42:37
I am in process of setting up my 1st MVC Web App. I know I need to provide a Forms Based Authentication model as well as I know I will be reusing it for multiple other internal web apps as well. All the documentation for MVC 5 Authentication, which I believe is all based on OWIN stuff, have it "baked" into a single web app using EF Code First no less. What I am trying is to have an another Web App that I strip everything out of except for the Account stuff and then try to "point" my web apps Authentication to that and have it return a "token", I'm guessing, of my Authenticated User and his/her

User Lockout in .net 4.5.1 ASP.NET MVC 5

醉酒当歌 提交于 2019-12-03 04:25:29
问题 So in the new .Net Framework 4.5.1 AspNetUser table does not have a column to lock user out after a finite number of unsuccessful attempts. Are there frameworks or solutions built for that purpose to replace the functionality that once existed in previous .net frameworks? Or do I have to build my own? 回答1: In the upcoming 2.0 Identity release, there is support for Account Lockout Configuration: manager.UserLockoutEnabledByDefault = true; // Enables ability to lockout for users when created

Could not load file or assembly 'WebMatrix.Data`

别说谁变了你拦得住时间么 提交于 2019-12-03 04:15:29
I updated MVC4 to MVC5. When you run the application I got an error that I don't have WebMatrix.Data . I installed it PM> Install-Package WebMatrix.Data . Now when you start getting in the browser: Could not load file or assembly 'WebMatrix.Data, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35' or one of its dependencies. Located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) Anyone know how to fix this? Package Manager Console Log Attempting to resolve dependency 'Microsoft.AspNet.WebPages.Data (≥ 2.0.20710.0)