I have an ASP.NET MVC 4 application that uses forms authentication (I started with the ASP.NET MVC 4 mobile template). I have also added display modes functionality to support mobile devices. However, forms authentication doesn’t seem to work with these different display modes.
In Global.asax Application_Start, I have
DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("iPhone")
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)
});
to register a display mode for an iPhone.
In ~/Views/Shared, I have _Layout.cshtml for desktop browsers and _Layout.iPhone.cshtml for iPhones. In ~/Views/Home, I have Index.cshtml for desktop browsers and Index.iPhone.cshtml for iPhones. In ~/Views/Account, I have the standard .cshtml files that come with the ASP.NET MVC 4 mobile web template (Index, Login, Register, etc.).
In FilterConfig.cs RegisterGlobalFilters, I added filters.Add(new System.Web.Mvc.AuthorizeAttribute()); so that authentication is needed to navigate anywhere in the site (except the AccountController, which has [AllowAnonymous] on the Login and Register action methods).
This is all basic stuff and set up correctly. When navigating to the site in the browser (from both a desktop browser and an iPhone browser), the display modes settings/configuration works great and I’m taken to either the normal .cshtml files or the iPhone-specific files.
The problem occurs when adding forms authentication.
When I first go to the site, since I’m not authenticated, I’m redirected to the Login page (as expected). The URL shows as http://localhost:63087/Account/Login?ReturnUrl=%2f . After logging in, I’m redirected back to the root and on to the correct display mode. All fine.
However, the URL doesn’t change. It stays as http://localhost:63087/Account/Login?ReturnUrl=%2f . This is a problem. Now any future navigation gets confused and sends me back to the Login page (even though I’ve already logged in and was authenticated).
How can this problem be fixed?
How can an ASP.NET MVC 4 application with multiple display modes be made to work correctly with forms authentication?
Thanks.
Related
I am upgrading an application that works on an intranet, from web forms (framework 4.8) to razor page (net 6). To get a windows user using web form, do the following:
Session("Usuario") = HttpContext.Current.Request.LogonUserIdentity.Name
But I am seeing that in razor page it is not so simple; I have already set windowsAuthentication to true and anonymousAuthentication to false in the launchSetting.json file, and what it shows me is a screen (see image) for the user to enter their authentication credentials, which is not what I require, since that the user has previously validated the domain (by turning on his machine every morning in the company).
Image
I see that with the following instruction the network user is obtained in Razor, and it works, but as long as I validate myself previously on the image screen.
var username = HttpContext.User.Identity.Name;
I have also tried adding the following in Program.cs, but the same always shows that screen.
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = options.DefaultPolicy;
});
app.UseAuthentication();
app.UseAuthorization();
Please if you can tell me how to get the windows user without registering again, or in any case indicate if this is the normal way of working in Razor Page.
Thanks.
I have a asp.net mvc application deployed in production. User can save a form in draft state and then can submit the form later. This functionality is working perfectly but for two of our users in Malaysia, when they click on Submit button, nothing is happening. It seems that the button is not working (page is not posted back).
I am unable to understand what could be the possible cause. If there is a firewall issue, then site would not have been opened at all.
Can I use fiddler to check or if there are some other options?
The Problem: I used to be able to login to my MVC4 website using Google external login but suddenly the "Google" button doesn't appear. I get no such behavior when I try a new clean MVC4 project.
Debugging shows that the IsAuthenticatedWithOAuth property of the OAuthWebSecurity object throws a "Missing Method Exception" when ExternalLoginsList action in the AccountController tries to return the _ExternalLoginsListPartial view. Then, when rendering the _loginPartial view, the Request.IsAuthenticated is false. Also, the generated HTML includes the code for the button (so it means it's an authentication issue?)
The website is supposed to run on windows Azure but the problem occurs both in the cloud and when running locally. Actually the first time the button disappeared was after publishing a new version of the website to Azure.
3 hours of going round and round this and I'm not any closer to a solution.
Badly need help here. Thanks
I am fairly new to mvc and I tried mvc4 when it was still in beta. I remember that when I started a new internet project, I got a popup dialog for log in and registration by default. I don't see that when I start a new project anymore, is that gone from the internet project?
I believe the popup dialog you're referring to was just the jQuery UI Dialog that the sample project used to use:
The project was built to support both a straight request to /Login and one via the jQuery UI dialog which was slimmed down to look like it was only a popup. It did this by making the Login action return a different View based on it being requested through the frame or not as detected by a value in the query string.
You can of course have this again, you'll just need to do it manually (or dig out one of the older templates) as it's no longer in the default templates.
To help you out, here's a couple of somewhat related questions that contain the sample code (ContextDependentView is one thing I remember from this template) and probably some hints on how to recreate it:
Generating a modal jQuery partial view with MVC4 does not work
ASP.NET MVC 4 and ContextDependentView
MVC4 - ContextDependentView - What does it mean?
It was removed from the Internet project templates in the final releases of MVC 4. The popup was pretty slick but I imagine there were issues/complexities they decided to eliminate by just keeping the view/page for logon and registration. In the older versions that had the pop-up they still had the view/page for logon because of how forms based authentication works. If the user is not authenticated/authorized for a web site/page MVC does a redirect to the logon page. This will not work with a JQuery popup dialog on the same page. The popup only worked if you clicked on the Logon link for the page. I am guessing that they decided since they need the view/page logon anyway to simplify things and keep it consistent by eliminating the popup dialog.
I have implemented an MVC Single Page Application (SPA) that only uses a JQuery dialog for a popping up a dialog for logon, and eliminates the need for logon page. But it required a fair amount of customization to authentication/authorization process on the server and used basic authentication on the client.
i have an asp.net mvc website. http:/mywebsite.com (just for this example)
I have been testing this asp.net mvc website locally on my machine and everything works fine on my local machine during testing. When i deploy to my web server
when i bring up http://mywebsite.com it goes to HomeController and load the view Views/Home/Index.aspx (which is perfect). So the main default page is fine.
The issue is, if i click any other other links (again that all seem to work fine locally) i get the following error:
The page cannot be found
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
Please try the following:
* Make sure that the Web site address displayed in the address bar of your browser is spelled and formatted correctly.
* If you reached this page by clicking a link, contact the Web site administrator to alert them that the link is incorrectly formatted.
* Click the Back button to try another link.
HTTP Error 404 - File or directory not found.
Internet Information Services (IIS)
some examples would be:
http:/mywebsite.com\Photos
http:/mywebsite.com\Links
there definitely are controllers and views setup for this (or it wouldn't work locally).
does anyone have any idea how this might work fine locally but somehow on the webserver it doesn't
Are you running the site on II7 in integrated mode? If not, you need to add a wildcard handler so that all URLs get mapped to your application. See Phil Haack's blog post on running ASP.NET MVC ON IIS 6.
Something else to watch out for. I had a static website - deployed on IIS 7.5. Added some MVC Controllers to handle some simple server side apps and then re-deployed it. I didn't copy the web.config file, because I wrongly assumed that everything that was needed was in the one that was automatically created by the server.
You must copy the web.config up as well.