I'm working on a website in ASP.NET and unfortunately I have to use MySql to store the site's data.
I'm really struggling to get MySql work with the CreateUserWizard functionality, and I'm just losing it.
Until I defined custom properties in the Web.Config, everything was fine, and I actually managed to register some fake users. Then I wanted to add some other properties, like address, gender, and so on. I added those properties in the Web.Config like this:
Web.Config
<profile enabled="true" defaultProvider="MySqlProfileProvider">
<providers>
<clear/>
<add name="MySqlProfileProvider" autogenerateschema="True" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.8.3.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D" conectionStringName="connMySql" applicationName="/"/>
</providers>
<properties>
<add name="Nome" type="string"/>
<add name="Cognome" type="string"/>
<add name="Sesso" type="string"/>
<add name="DataNascita" type="datetime"/>
<add name="Indirizzo" type="string"/>
<add name="Citta" type="string"/>
<add name="Provincia" type="string"/>
<add name="CAP" type="int"/>
</properties>
</profile>
Please note that my Connection String is correct, since I used it in other parts of my Web.Config and runs just fine.
Later on, in my sign up page, I wrote this code (after validating the input, of course), to update the created user.
register.aspx.cs
protected void wizard1_CreatedUser(object sender, EventArgs e)
{
ProfileCommon p = (ProfileCommon)ProfileCommon.Create(wizard1.UserName, true);
p.Nome = ((TextBox)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("Nome")).Text;
p.Cognome = ((TextBox)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("Cognome")).Text;
p.Sesso = ((DropDownList)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("Sesso")).SelectedValue;
p.DataNascita = System.Convert.ToDateTime(((TextBox)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("DataNascita")).Text);
p.Indirizzo = ((TextBox)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("Indirizzo")).Text;
p.Citta = ((TextBox)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("Citta")).Text;
p.Provincia = ((TextBox)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("Provincia")).Text;
p.CAP = System.Convert.ToInt32(((TextBox)wizard1.CreateUserStep.ContentTemplateContainer.FindControl("CAP")).Text);
p.Save();
}
All the controls are well positioned into the .aspx page.
So, when I run the page, I can see the form and type the data in. Then my code validates the input, and if it's all correct, it creates the user and gets into the wizard1_CreatedUser function, where it stops at the p.Nome = ... part, which actually is the first instruction that makes use of the MySqlProfileProvider.
It just says Host 'Hostname' is not allowed to connect to this MySQL server BUT MySqlRoleProvider and MySqlMembershipProvider work just fine. In fact, the user is created, but my custom profile data aren't, since the code stops there.
I'm using a DB User with all the privileges.
I'm probably missing something, but I don't really know what.
Thank you.
Turns out that I needed to enable Full Trust.
Also, some files were mispositioned (they were not in the very root of the website).
Related
I have a small programm in which i copy paste stuff from A to B on the PC. The directory paths are written in the config, and when i (in the application) change the directory in the textbox it is updating the config file. I checked it, the value is immediatlely rewritten at the appropriate key. When i close the app and reopen it, it is updatet to the previously changed directory path, but i dont want to have to close the application and reopen is. I have a combobox and i want it to update as soon as the combobox reselect event triggers. But during the runtime (altough it is already changed in the config) it will not update the directory path shown in the app.
I read through and tried everything i found online and sadly nothing helped. Not every every kind of
ConfigurationManager.RefreshSection("appSettings");
THis is my config:
<appSettings file="">
<clear />
<add key="SourcepathClient" value="D:\xxx" />
<add key="SourcepathWin32" value="D:\xxx" />
<add key="DestinationpathUpdatePackages" value="D:\xxx" />
<add key="DestinationpathClient" value="D:\xxx" />
<add key="5_9_0-DestinationpathClient" value="D:\xxxt" />
<add key="5_9_0-DestinationpathUpdatePackages" value="D:\xxx" />
<add key="5_9_1-DestinationpathClient" value="D:\xxx" />
<add key="5_9_1-DestinationpathUpdatePackages" value="D:xxx" />
<add key="5_9_2-DestinationpathClient" value="D:\xxx" />
<add key="5_9_2-DestinationpathUpdatePackages" value="D:\xxx" />
</appSettings>
and this the code:
Configuration config = ConfigurationManager.OpenExeConfiguration(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "UpdatePackager.exe"));
config.AppSettings.Settings[ComboBoxVersion.Text + "-DestinationpathClient"].Value = TextBoxDestinationpathClient.Text;
config.AppSettings.Settings[ComboBoxVersion.Text + "-DestinationpathUpdatePackages"].Value = TextBoxDestinationpathUpdatePackage.Text;
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("appSettings");
I hope someone can help me.
Regards
i think there is no issue with code it issue related your access, it makes a difference if you run your application in IIS and run your test sample from Visual Studio. The ASP.NET process identity is the IIS account, ASPNET or NETWORK SERVICES (depending on IIS version).
Might need to grant ASPNET or NETWORK SERVICES Modify access on the folder where web.config resides.
I am trying to set up an ASP.NET MVC site to use LDAP. I feel like I have everything set up right, but Membership.ValidateUser keeps returning false when I am expecting it to return true. I've read some documentation, like for example this one on AD membership, but one thing I want to make sure I completely understand as I am troubleshooting is this:
Is the defaultProvider attribute value something specific provided by Microsoft? Or is it just an arbitrary name, and we can call it anything?
This is my current web.config setup:
<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://dc1.dc2.dc3.dc4/DC=dc1,DC=dc2,DC=dc3,DC=dc4" /
</connectionStrings>
<membership defaultProvider="AspNetActiveDirectoryMembershipProvider">
<providers>
<clear />
<add name="AspNetActiveDirectoryMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
</providers>
</membership>
I am using client application services to authenticate users for windows app. (SqlMembershipProvider) Login works fine and I can grab the user name by using the following code:
userName = System.Threading.Thread.CurrentPrincipal.Identity.Name.ToString();
I want to grab a bit more info about the user such as the email address. I have tried the following line to get the user.
MembershipUser mu = Membership.GetUser();
The above line gives me a "specified method is not supported" message. Same result if I pass in the userName as well.
Is there a way to get more detailed user info using client application services as you would in an asp.net app? If not, I wonder if I should just create my own Web Service wrapper instead?
I'm using the 4.0 Framework.
Update: A slice of the app.config file
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="http://PC-03/FAAppServices/Authentication_JSON_AppService.axd" credentialsProvider="BillingFormsApplication.LoginForm, BillingFormsApplication" savePasswordHashLocally="True" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="http://PC-03/FAAppServices/Role_JSON_AppService.axd" cacheTimeout="1209600" honorCookieExpiry="True" />
</providers>
</roleManager>
</system.web>
Update 2: Stack Trace
at System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider.GetUser(String username, Boolean userIsOnline)
at System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline)
at BillingFormsApplication.MDIMain.MDIMain_Load(Object sender, EventArgs e) in C:\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.cs:line 340
SimpleMembership, as the name implies, is very simple and does not implement all of the MembershipProvider methods; just those required for MVC basic authentication and authorization. If you try to use a method that is not implemented you will get that exception. You can either create your own MembershipProvider from scratch or you can inherit from SimpleMembership and implement the methods that are not implemented.
The exception is by design
The class ClientAuthenticationMembershipProvider does not use the method GetUser.
If your app.config is configured to use ClientAuthenticationMembershipProvider, and you call Membership.GetUser, then you will always get a NotSupportedException.
ref: https://learn.microsoft.com/en-us/dotnet/api/system.web.clientservices.providers.clientformsauthenticationmembershipprovider.getuser
I am using ASP.net membership, but I want to store additional information for client while registration instead of default columns as membership provider does, so how can I customize the dbo.aspnet_Users table and change the code so that it doesn't affect the other functionality and works fine?
Could somebody suggest me on it how can I achieve this?
Use an ASP.NET Profile-Provider.
https://web.archive.org/web/20211020111657/https://www.4guysfromrolla.com/articles/101106-1.aspx
You can store any kind of additional information even binary(images).
I've used The SqlProfileProvide myself in my currect application to let the user chose his startpage self.
Therefor i only needed to add this to the web.config:
<profile defaultProvider="AspNetSqlProfileProvider">
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="RM2ConnectionString" applicationName="/ERP"/>
</providers>
<properties>
<add name="Startpage"/>
</properties>
</profile>
And i could write this property in codebehind:
if(User.Identity.IsAuthenticated)
{
HttpContext.Current.Profile.SetPropertyValue("Startpage", startPage); //startPage is a String
HttpContext.Current.Profile.Save();
}
and read it in the following way:
if(User.Identity.IsAuthenticated)
{
Dim user = Membership.GetUser();
Dim startPage = HttpContext.Current.Profile.GetPropertyValue("Startpage") as String;
}
You can store anything you want, see the link above for further informations.
I am using the Profile feature of ASP.NET in a website. Updating a profile is working weirdly! A user can't update his/her own profile, neither the web site user nor the administrator, but, the administrator is able to update profiles of other users.
In the backend, after Profile's save() is called, SQL Server traces show that aspnet_Profile_SetProperties stored procedure is called twice. First, with new values, then, with old values. The second execution is done after page unload. My code has nothing to do with transactions.
Why is it working so weirdly?
Could there be an issue with aspnet_regsql's installation as I have installed uninstalled and again installed it!?
Code
web.config
<authentication mode="Forms">
<forms name="FormsAuthentication" loginUrl="~/Login.aspx" defaultUrl="~/Login.aspx" timeout="20"/>
</authentication>
<membership defaultProvider="CustSqlMembershipProvider">
<providers>
<add connectionStringName="connString" applicationName="/space_online" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" name="CustSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider="CustSqlRoleProvider">
<providers>
<add connectionStringName="connString" applicationName="/space_online" name="CustSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>
</providers>
</roleManager>
<anonymousIdentification cookieless="AutoDetect" enabled="true"/>
<profile defaultProvider="CustSqlProfileProvider" enabled="true">
<providers>
<add name="CustSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="connString" applicationName="/space_online"/>
</providers>
<properties>
<add name="FirstName" type="System.String"/>
<add name="LastName" type="System.String"/>
<add name="Email" type="System.String"/>
<group name="Address">
<add name="Street" type="System.String"/>
<add name="City" type="System.String"/>
<add name="PostalCode" type="System.String"/>
</group>
<group name="Contact">
<add name="Phone" type="System.String"/>
<add name="Mobile" type="System.String"/>
<add name="Fax" type="System.String"/>
</group>
<add name="ShoppingCart" type="psb.website.BLL.Store.ShoppingCart" serializeAs="Binary" allowAnonymous="true"/>
</properties>
</profile>
Code behind
private void UpdateProfile(ProfileCommon myprofile)
{
myprofile.FirstName = tbFirstName.Text.Trim();
myprofile.LastName = tbLastName.Text.Trim();
myprofile.Email = tbEmail.Text.Trim();
myprofile.Address.Street = tbStreetPhysical.Text.Trim();
myprofile.Address.City = tbCity.Text.Trim();
myprofile.Address.PostalCode = tbPostalCode.Text.Trim();
myprofile.Contact.Phone = tbPhone1.Text.Trim();
myprofile.Contact.Mobile = tbMobile.Text.Trim();
myprofile.Save();
}
private ProfileCommon GetProfile()
{
ProfileCommon profile = this.Profile;
if (Request.QueryString["UserName"] != null && HttpContext.Current.User.IsInRole("Admin"))
profile = this.Profile.GetProfile(Request.QueryString["UserName"].ToString());
else
profile = this.Profile.GetProfile(HttpContext.Current.User.Identity.Name);
return profile;
}
protected void tbUpdateProfile_Click(object sender, ImageClickEventArgs e)
{
UpdateProfile(GetProfile());
}
The profile is by default automatically saved at the end of the execution of an ASP.NET page, see the profile Element (ASP.NET Settings Schema) documentation on this. This explains the second "mysterious" save that you observe.
You can try to change automaticSaveEnabled to false.
all the transactions must be done before page unload.
if page is unloaded then its all function calls will also be removed or stopped in midway.
Access to ProfileCommon should be done through HttpContext.Current.Profile as that is a reference to the current user's profile (logged in or anonymous) and you don't need to explicitly call Save. Try this:
private void UpdateProfile()
{
var myprofile = HttpContext.Current.Profile as ProfileCommon;
if (profile == null) {
throw new InvalidOperationException("HttpContext.Current.Profile is not of type ProfileCommon for some reason!");
}
myprofile.FirstName = tbFirstName.Text.Trim();
myprofile.LastName = tbLastName.Text.Trim();
myprofile.Email = tbEmail.Text.Trim();
myprofile.Address.Street = tbStreetPhysical.Text.Trim();
myprofile.Address.City = tbCity.Text.Trim();
myprofile.Address.PostalCode = tbPostalCode.Text.Trim();
myprofile.Contact.Phone = tbPhone1.Text.Trim();
myprofile.Contact.Mobile = tbMobile.Text.Trim();
}
You may need to clear the default provider in your web.config.
Like this:
<profile defaultProvider="CustSqlProfileProvider" enabled="true">
<providers>
<clear /><!-- here -->
<add name="CustSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="connString" applicationName="/space_online"/>
</providers>
<properties>
<...>
</properties>
</profile>
Here is a good explanation for this:
Removing existing profile providers
And here is another good site:
http://odetocode.com/articles/440.aspx