I have a web application and I need to provide its users the option to switch login method from FormsAuth to WindowsAuth. I managed to change the web.config file via code:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Url.Content("~"));
AuthenticationSection auth = ((AuthenticationSection)(config.SectionGroups["system.web"].Sections["authentication"]));
auth.Mode = AuthenticationMode.Windows; // Or Forms if I want to.
config.Save();
But the problem is, when I use FormsAuth, I need the Anonymouse Authentication option to be turned on, and when I use WinAuth, I need it to be off. And I just cannot find the way to change that option via code.
Everything on the internet says to do this:
<security>
<authentication>
<anonymousAuthentication enabled="false/true" />
</authentication>
</security>
But when I insert this into my webapp's web.config it says that configuration is wrong. Than I read this might work in another config file, like appHost.config or something like that but I prefer to make changes only to my own application and not to IIS I hope you understand why.
So, how can I do that?
You are trying to update wrong section. anonymousAuthentication is part of system.webServer and not system.web. Correct configuration is
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
You can modify it using ServerManager class found in Microsoft.Web.Administration. Search for nugget package "Microsoft.Web.Administration". Once you have added reference to Microsoft.Web.Administration.dll using nugget you can modify it using code as follows:
using (ServerManager serverManager = new ServerManager())
{
Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration();
Microsoft.Web.Administration.ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication");
anonymousAuthenticationSection["enabled"] = true;
serverManager.CommitChanges();
}
Okay, so, turned out I could not find a way to dynamically change the auth mode, but I found pretty neat solution to the problem: I have created another web application, nested it inside the first one, had set Forms Auth mode for the first one and Windows for the nested and whenever I needed to use IIS's power to work with domain, user groups etc, I used a redirect from one to another, passing data by cookies and as url parameters.
Related
I discovered something unexpected with my ASP.NET WebApp (Visual 2017 .NET Framework 4.6.1) in a production environment.
I have a model with a property StringDato, which is a STRING following the pattern dd.mm.yyyy hh.mm.ss (the german/european format).
Having faced some issues for parsing, I decided to work with strings for my dates, so there is no a single part of my code where I refer to a DateTime object.
In my development and testing environments (2 different IIS servers), StringDato is displayed as it should be. But in the production environment (a third IIS server) it always shows US formatted.
For example, 23.09.2017 17.45.00 will become 9/23/2017 5:45:00 PM. This happens no matter what the browser is (IE/Edge/Chrome).
The browser is german/european configured, and so is the user's system. The only thing I don't know about yet is how my production server is configured.
I'm trying to get the logic here. Does it make sense to assume the server could "recognize" a string as a date because of its pattern, display it as a date, and format it according to it's config and not the user's settings? What else can I look at to fix this?
It´s simple. Keep Invariant Culture Info on Web.Config:
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<globalization uiCulture="de" culture="de-DE" />
</system.web>
I need to log events in Json format using Serilog. I want to use RollingFileSink.
According to the following link, we cannot specify TextFormatter in App.config file.
How to specify JsonFormatter in Web.config for SeriLog?
Is is possible to specify all other configurations like file path in app.config and then add textformatter as JsonFormatter in code while initialing the logger?
Just using plain settings works well for this, e.g.:
<add key="filePath" value="C:..." />
And:
WriteTo.RollingFile(..., ConfigurationManager.AppSettings["filePath"])
I have one project for publishing two separate websites on different servers. Both websites is in different languages (En and tr).
For web-deploy I want to change appsettings for different environments for Debug and Release (For both websites).
I added config files for transformations like this:
Appsetting in main Web.config:
<add key="WebsiteMainCulture" value="en-US"/>
The code in transformation files are accordingly like this:
<appSettings>
<add key="WebsiteMainCulture" value="TR"
xdt:Locator="Match(key)" xdt:Transform="Replace"/>
</appSettings>
I selected Web.TR.Debug.config and ran project, But I always get 'en-US' as WebsiteMainCulture.
string website_main_culture = ConfigurationManager.AppSettings["WebsiteMainCulture"].ToString(); // always en-US
Please help. Is there any better approach suggested for same environment?
Thanks!
I got the solution for the problems I asked, also related to my previous question here:
https://stackoverflow.com/questions/30612865/selecting-correct-seed-method-for-particular-language
I just added config transformation files by right clicking, to relevant publish(web deploy) files.Then added desired values to keys. And it worked.
Here are config transformation file auto generated.
We are using spring security and have it working well. I am trying to figure out one thing that has not being obvious - how do I configure ldap-server attribute to use different url based on deployed environment?
This is what I have that is working:
<ldap-server url="ldap://testserver:port/o=blah" manager-dn="cn=bind,ou=Users,o=blah" manager-password="password"/>
<authentication-manager id="authenticationManager" alias="authenticationManager">
<ldap-authentication-provider
user-search-filter="(cn={0})"
user-search-base="ou=Users"
group-search-filter="(uniqueMember={0})"
group-search-base="ou=groups"
group-role-attribute="cn"
role-prefix="none">
</ldap-authentication-provider>
Now, how do I configure it to use a different url based on deployed environment?
thanks in advance,
Sharath
I've done that with Spring profiles:
In your spring.*.xml config file use this at the end of your file:
<beans profile="production">
...
</beans>
<beans profile="local">
...
</beans>
As VM Arguments the used profile must be provided:
-Dspring.profiles.active=production
Regards
You can use the url as variables and set them in a properties file.
To change the properties file should be easier. I know you can do that with Maven - with jar or war plugin depending on packaging, including generating two (or more) packages with one execution - but I suppose you can with Ant or other managers too.
Of course, you could use that solution to change the whole xml, but it's easier to do that with a properties file because that way, when changing the configuration, the markup will not be in the way, only variables and values.
my site uses JSF and the url appears to be, http://mysitename.com/wompower6/faces/home.xhtml
I am using prettyfaces, so if I use the following in pretty-config.xml, i can change the name to http://mysitename.com/wompower6/home
<url-mapping id="home">
<pattern value="/home" />
<view-id value="/faces/home.xhtml" />
</url-mapping>
my questions are
how can i remove the application
name wompower6 , so that the url
becomes mysitename.com/home ?
in my web.xml, i have
<welcome-file>home.xhtml</welcome-file>,
but this does not seem to work. When
i type, mysitename.com, it does not
get mapped to home.xhtml. any clue
here?
how can i remove the application name wompower6 , so that the url becomes mysitename.com/home?
This is a webapp <Context> setting and configuration is dependent on the servletcontainer used. If you're for example using Tomcat, then there are basically 2 options to make your webapp the root webapp.
Rename the WAR file to ROOT.war and Tomcat will by default deploy it on context root.
Set path attribute of <Context> element in Webapp/META-INF/context.xml (or Tomcat/conf/server.xml, depending where you'd like to define it) to an empty String. E.g.
<Context path="" ...>
Other containers support similar constructs. Consult their documentation for detail. If you're using an IDE like Eclipse, then you can also set it in the Web Project Settings property of the project properties (rightclick project and choose Properties). Set the Context root value to just /.
in my web.xml, i have home.xhtml, but this does not seem to work. When i type, mysitename.com, it does not get mapped to home.xhtml. any clue here?
I assume that you're talking about the <welcome-file> setting. This has to point to a physically existing file, not to a virtual URL, such as /faces/*. There are basically two ways to overcome this:
Provide a physically existing /faces/home.xhtml file (it can even be left empty).
Replace the ugly /faces/* URL pattern of the FacesServlet mapping in web.xml by *.xhtml so that it will just kick in on every request for a XHTML file.
<url-pattern>*.xhtml</url-pattern>
This way you don't need to fiddle with /faces/* URL patterns.