Custom MembershipProvider Initialize method - c#

When overriding the MembershipProvider and calling it directly, is there a way to fill the NameValueCollection config parameter of the Initialize method without manually looking through the config file for the settings?
Obviously this Initialize is being called by asp.net and the config is being filled somewhere. I have implemented my own MembershipProvider and it works fine through the build in controls.
I would like to create a new instance of my provider and make a call to it directly, but I don't really want to parse the .config for the MembershipProvider, it's connection string name and then the connection string if it's already being done somewhere.

tvanfosson- Thanks for the help. (if I had the 15 points necessary I would vote you up)
From your link I was able to figure it out. It turns out the second parameter to the Initialize proceedure was the list of parameters from the provider and could be reached in the following way:
string configPath = "~/web.config";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
MembershipSection section = (MembershipSection)config.GetSection("system.web/membership");
ProviderSettingsCollection settings = section.Providers;
NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters;
Initialize(section.DefaultProvider, membershipParams);

Not sure why you want to create a new one, but if you create it yourself, you'll need to read the web config and get the values yourself to pass to Initialize() as this is done outside the class. I'm sure, though, that there is already a section handler for this section so it should be just a matter of doing:
MembershipSection section = WebConfigurationManager.GetSection("membership");
Then find your provider and accessing its properties to construct the NameValueCollection. I don't think you will have to write any code to parse the configuration section.
Here is the MembershipSection documentation at MSDN. Drill down from there.

In any case you shouldn't create instance of MembershipProvider. It is creating and initializating by standard asp.net infrastructure. You can access to it by code like this one:
var customerMembership = Membership.Provider;

Related

Inject AddSqlServer with another service

Is it possible to resolve an instance of ISettingsService from the ConfigureServices method in Startup(.cs) - webapplication?
I've implemented a SettingsService which is able to retrieve the database connectionstring from an external source (secure credentials store). Within the ConfigureServices I need an instance of the ISettingsService in order to get the connectionstring and pass it to the services.AddSqlServer<MyDbContext>(connectionstring) method.
While creating the instance (using var provider = services.BuildServiceProvider(); var settings = provider.GetService<ISettingsProvider>();) Visual Studio displays the next error:
Another developer posted a similar question on StackOverflow and the answer provides a solution in case of AddSingleton/ AddTransient. What is the correct way to apply it on the AddSqlServer call? Or could you provide another solution to avoid the warning/ error message?
The Intellisense comment for .AddSqlServer actually says to use .AddDbContext if you need more control, and that's certainly the correct option.
If you refer to the source code here, you can see that all .AddSqlServer is actually doing is calling .AddDbContext and configuring the options accordingly. We can therefore write our own solution like this:
services.AddDbContext<DbContext>((serviceProvider, options) => {
var settings = serviceProvider.GetService<ISettingsProvider>();
// I don't know what methods your ISettingsProvider makes available
// so adjust accordingly
string connectionString = settings.GetSetting("connectionString");
options.UseSqlServer(connectionString);
});
Of course you can make other changes to the options here, and .UseSqlServer can also take a Action<SqlServerDbContextOptionsBuilder> (options.UseSqlServer(connectionString, opts => opts.EnableRetryOnFailure()), etc.) to further configure it.

C# AWS Parameter Store - Configuration not loading SystemManagerConfiguration in .Net 6

Application is not able to talk to AWS Parameter Store in .NET 6.
It is always talking to appsettings.json.
I tried debugging locally, still same behavior. Not able to find the SystemManagerConfiguration under list of configuration .
var builder = WebApplication.CreateBuilder();
var connectionString = builder.Configuration.GetConnectionString("OrderTrackerDatabase");
Packages Used
Library Source Code : https://github.com/aws/aws-dotnet-extensions-configuration
image
I got the same issue and finally resolved it.
The samples code in https://github.com/aws/aws-dotnet-extensions-configuration missed one line as below after called "AddSystemsManager" method in .Net 6.
builder.Services.Configure<Settings>(builder.Configuration.GetSection($"common:settings"));
After added above line, then I'm able to get the correct values from AWS Parameter Store when using the settings.
I've also created an issue of this in GitHub as below -
https://github.com/aws/aws-dotnet-extensions-configuration/issues/114
I believe the problem might be the trailing slash after "/OrderTracking/", try "/OrderTracking" instead.
WebApplication.CreateBuilder() will create new instance and doesn't carry over the SystemManager configuration.
Instead, use IConfiguration instance through constructor DI.
var connectionString = _configuration.GetConnectionString("OrderTrackerDatabase");
In my case this extensions method was returning null at my lambda:
private static IConfiguration InitializeConfiguration() => new ConfigurationBuilder()
.AddSystemsManager($"/my-data", true, TimeSpan.FromMinutes(5))
.Build();
Because the role of lambda didn't have permission for read SSM for that resource.
User: is not authorized to perform: ssm:GetParametersByPath on resource
So, just add necessary permission (ssm:GetParametersByPath) for the role of lambda to your resource at System Manager.
In my case, I am using lambda serverless, so the IConfig is always null when it is passed to the controller.
I resolved it by changing the IOptions<Settings> settings in the Controller constructor to IConfiguration settings and then access the parameters by name like _settings.GetValue<string>("ParameterName")
A little less "Object Oriented", but it seemed much easier than this complex solution

Get OrganizationServiceProxy from CrmOrganizationServiceContext in external MVC app

I'm want to be able to set the caller ID on my
XrmServiceContext : Microsoft.Xrm.Client.CrmOrganizationServiceContext
Context that has been generated for crm using svcutil.exe.
As far as I can tell I cant do this on an existing connection and I need to first create an instance of OrganizationServiceProxy set the CallerID and then pass it as a paramater to a new XrmServiceContext which I can then use instead.
However I'm kind of stuck on how I go from a CrmOrganizationServiceContext to having a OrganizationServiceProxy
The program is a separate .Net4.5 application
Any helpful tips or links?
Edit: Found this page just after posting this: http://msdn.microsoft.com/en-us/library/gg695810.aspx
So it may be as simple as:
var connection = new CrmConnection("Xrm");
connection.CallerId = uide;
_serviceContext = new XrmServiceContext(connection);
Edit 2: It was not as simple as that.
Doing this resulted in no change of what data I received.
CrmConnection connection = new CrmConnection("Xrm");
connection.CallerId = Guid.NewGuid();//u.Id;
_serviceContext = new XrmServiceContext(connection);
It compiles and dosen't crash but I was suspicious when I used the id of a user with very low privledges but still got all data back, I then tried generating a new Guid for every instance of the XrmServiceContext but I am still getting everything back. So I am guessing it is not being used.. or I am missing something else.
Edit 3
Doing a WhoAmIRequest after the CallerID has been set still returns the same user that is set in the connection string.
Edit 4
Seems my problems are Cache related.
In my implementation I need to first make a call to the service context to figure out the Guid of the user I want to impersonate. This call is made without CallerID set. If I skip this initial query and just set a specific Guid from the beginning the CallerID works. I'm guessing this is because the service context has cached my original CallerId or something similar.
Now I just have to figure out how to clear the cache in CRM 2013 SDK.
Edit 5
By turning of the cache completly using this guide: http://msdn.microsoft.com/en-us/library/gg695805.aspx I have gotten it to work. I would however prefer if I could just clear it out at the one point I need to instead of disabling it completly.
If someone can show me how to empty the service context cache using code I will mark that as the correct solution
There is a method that can be used when dealing with your "_serviceContext"
You should be able to use: _serviceContext.ClearChanges(); This clears all tracking of a particular entity within the Cache. See the Public Methods Section
The problem is related to the default instanceMode that is defined in the web.config under the microsoft.xrm.client section.
By default, the setting is set to PerRequest
PerRequest – returns the same first instance in the context of a Web
request, for example. one instance for each HttpContext instance.
So, in this case, when you do the initial call to work out which user you want to set the CallerId to, the instance is being 'cached' (for lack of a better word) and on subsequest calls within the same request, this instance is being returned, even if you are creating a new XrmServiceContext
The solution is to change the instanceMode to PerInstance
PerInstance – returns a new instance on each call.
Modify your web.config so that the instanceMode attribute is specified correctly
<microsoft.xrm.client>
<contexts>
<add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="Xrm" />
</contexts>
<services>
<add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client" instanceMode="PerInstance" />
</services>
</microsoft.xrm.client>
Found this information in the article posted by JensB in his 5th edit: http://msdn.microsoft.com/en-us/library/gg695805.aspx

Getting started with Google Adword .net API

Most things that I've found about the .net AdWords API mention creating a bunch of settings for the application that will be used when you create an instance of the AdWordsUser class; however, I've had no luck with this. My user always ends up nil and no settings are being read even though I named them as indicated in the AdWordsAppConfig class.
AdWordsAppConfig appConfig = new AdWordsAppConfig();
appConfig.AdWordsApiServer = "https://adwords-sandbox.google.com";
appConfig.Email = "fname.lname#gmail.com";
appConfig.Password = "password";
appConfig.DeveloperToken = "fname.lname#gmail.com++USD";
AdWordsUser user = new AdWordsUser();
The AdWordsUser has an overloaded constructor that accepts a dictionary of strings containing configuration parameters but doesn't have on that accepts an AdWordsAppConfig object.
http://code.google.com/p/google-api-adwords-dotnet/source/browse/trunk/src/AdWords/Lib/AdWordsAppConfig.cs
According to the AdWordsAppConfig class the following is used to retreive the settings when you call the constructor; however, how do you create a section called "AdWordsApi" in the App.Config? I don't see that option in the settings editor.
ReadSettings((Hashtable) ConfigurationManager.GetSection("AdWordsApi"));
Any ideas on the proper way to set the configuration information and create the user instance that I need?
What Eric mentioned is the long-term solution, but if anyone is looking for a quick answer, this is how you do it:
AdWordsUser user = new AdWordsUser();
(user.Config as AdWordsAppConfig).Password = "XXXXX";
//TODO (Add more configuration settings here.
CampaignService campaignService = (CampaignService) user.GetService(AdWordsService.v201109.CampaignService);
//TODO (Add your code here to use the service.)
Cheers,
Anash
You may want to post this question on the official AdWords API forum, as it is monitored by the current maintainers of that library:
http://code.google.com/apis/adwords/forum.html

How can I get the URL of the current page from within a C# App_Code class?

I have a logging class that, well, logs things. I would like to add the ability to automatically have the current page be logged with the messages.
Is there a way to get the information I'm looking for?
Thanks,
From your class you can use the HttpContext.Current property (in System.Web.dll). From there, you can create a chain of properties:
Request
Url and RawUrl
The underlying object is a Page object, so if you cast it to that, then use any object you would normally use from within a Page object, such as the Request property.
It's brittle and hard to test but you can use System.Web.HttpContext.Current which will give you a Request property which in turn has the RawUrl property.
public static class MyClass
{
public static string GetURL()
{
HttpRequest request = HttpContext.Current.Request;
string url = request.Url.ToString();
return url;
}
}
I tried to break it down a little :)
In the past I've also rolled my own logging classes and used Console.Writeln() but really there are a number of good logging options that already exist so why go there? I use NLog pretty much everywhere; it is extremely flexible with various log output destinations including console and file, lots of log format options, and is trivial to set up with versions targeting the various .net frameworks including compact. Running the installer will add NLog config file options to the Visual Studio Add New Item dialog. Using in your code is simple:
// declare in your class
private static Logger logger = LogManager.GetCurrentClassLogger();
...
// use in your code
logger.Debug(() => string.Format("Url: {0}", HttpContext.Current.Request.Url));

Categories