c# - ASP.NET machinekey set keys in code -
i want adjust machine keys dynamically in code during runtime, iis hosted asp.net mvc 4 website.
the machine keys, encryption , validation keys algorithms use, stored in database. instead of reading values web.config
file, want inject values during application startup , let system use instead.
is there way accomplish without having change web.config
@ (to change in memory configuration)?
i have tried accessing configuration section marked readonly , sealed, cannot override isreadonly()
. however, there setter
indicator, me, there may way potentially remove readonly flag.
var configsection = (machinekeysection)configurationmanager.getsection("system.web/machinekey"); if (!configsection.isreadonly()) { configsection.validationkey = _platforminfo.machinekey.validationkey; configsection.decryptionkey = _platforminfo.machinekey.encryptionkey; ... }
is there way accomplish this? alternative can see use custom method appharbor, rather stick built in approach if possible @ all.
in case asks why want that, reason is, large number of identical websites running in webfarm. hence, having non-auto-generated keys must (must same on each server). each website should isolated , should not share same keys. websites identical in physical representation, share same physical location. reason web.config file cannot contain application specific settings.
edit: helpful confirm, @ least, if not possible. said, 1 can use custom authentication , encryption methods avoid using machine key settings altogether. thanks.
there no way set programmatically once web application starts. however, still possible accomplish goal.
if each application running in own application pool, , if each application pool has own identity, check out clrconfigfile switch in applicationhost.config. can use per-application pool inject new level of configuration. see http://weblogs.asp.net/owscott/archive/2011/12/01/setting-an-aspnet-config-file-per-application-pool.aspx example of how use this. set explicit , unique <system.web/machinekey> element in each application pool's custom clr config file.
this same mechanism used azure web sites, godaddy, , other hosters need set default explicit machine keys on per-application basis. remember acl each target .config file appropriately application pool accessing it.
Comments
Post a Comment