using custom Profile provider in ASP.net MVC? - asp.net-mvc

im currently trying to implement a profile provider for my site a few days now and having a hard time working on it, im a php programer and i just shift to asp.net recently
Im using Linq to sql and follow this http://www.codeproject.com/KB/aspnet/LINQCustomProfileProvider.aspx tutorial.
the reason im using my own because i having different structure than any default of asp.net have. The profile data is inside my user table.
The compile was fine, login was fine.
but i tried
<% CMSProfile profile = HttpContext.Current.Profile as CMSProfile;%>
<%= profile.NickName %>
it won't work and throw me a System.NullReferenceException...
so how can i automatically get my Profile into the HTTPCONtext so that i can call out easily everytime.
If you need any more data, i can provide.
Thank you very much.
Web.config:
<roleManager enabled="false" defaultProvider="CMSRoleProvider">
<providers>
<clear />
<add name="CMSRoleProvider" type="P014.ProviderClass.CMSRoleProvider" connectionStringName="P014ConnectionString" applicationName="/" />
</providers>
</roleManager>

How have you registered the provider in web.config? You shouldn't have to instantiate the provider yourself it should be done by the app at startup. If you give more info I might be able to help.
EDIT: Here is my web.config, maybe it will be of help to you.
<profile defaultProvider="SWIntranetProfile" enabled="true">
<providers>
<clear/>
<add name="SWIntranetProfile" type="SWIntranetProfile"/>
</providers>
<properties>
<clear/>
<!-- SID is really LOGON_USER -->
<add name="SID" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="PersonID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
<add name="EmailAddress" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="Position" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="Name" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="FirstName" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="LastName" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="ImageName" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="PhoneExt" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="LastIP" allowAnonymous="false" type="System.String" readOnly="false"/>
<add name="IntranetTheme" allowAnonymous="false" type="System.String" readOnly="false"/>
<add name="UnionID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
<add name="UnionName" allowAnonymous="false" type="System.String" readOnly="true"/>
<add name="OfficeID" allowAnonymous="false" type="System.Int32" readOnly="true"/>
</properties>
</profile>

I noticed that the author of the article didn't give a code sample for binding the profiler or Workflow to the HttpContext.
Have you written your own class to do this? If so, have you set this up correctly in the web.config?
If your using IIS7 you also need to register your IHttpModule under the webServer section of the web.config file.
EDIT
To be able to run the code snippet you have show to you need to have placed your custom Profiler to the HttpContext.
You can do this in two ways, either on a per request basis or on the application start.
For the per request basis you would need to create a class that implements IHttpModule and register it in the web.config.
For the Application start you need to attach your CMSProfile to the HttpContext.Current in the Application_OnStart method.
Their is a sample application attached to the article you posted, have you downloading and checked sample application?

Related

The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons)

I hosted my MVC website in plesk by using FTP publish option of VS2013 and everything went fine, I mean all the required files were copied properly in the destination under httpdocs virtual directory. But I am getting below error now when I visit the URL:
I have tried all the possible solutions mentioned in many links but no proper solution I have got so for! Its making me real confusing about this!
I have tried to apply solutions in this and also checked this question but none of them were useful. My web.config file is as below:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=sometoken" requirePermission="false" />
</configSections>
<appSettings>
<!--<add key="MaintenanceMode" value="false" />-->
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="SessionTimeoutRedirect" value="true" />
<add key="isAjaxHandled" value="false" />
</appSettings>
<system.web>
<customErrors mode="Off" />
<trust level="Full"/>
<compilation targetFramework="4.0" defaultLanguage="c#"/>
<httpRuntime executionTimeout="1048576" maxRequestLength="100000" />
<globalization culture="en-IN" uiCulture="en-IN" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" defaultUrl="~/Admin/Index" timeout="2880" protection="Encryption" slidingExpiration="true" cookieless="AutoDetect" />
</authentication>
<pages controlRenderingCompatibilityVersion="4.0" />
</system.web>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="MCBConnectionString" connectionString="metadata=res://*/Models.EntityDataModel.MCBEntityModel.csdl|res://*/Models.EntityDataModel.MCBEntityModel.ssdl|res://*/Models.EntityDataModel.MCBEntityModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=somesource;Network Library=;Packet Size=4096;Integrated Security=no;User ID=uid;Password=pwd;Encrypt=yes;TrustServerCertificate=True; integrated security=no;connect timeout=120;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
Below is the file structure in Plesk Admin panel
One more thing to note: If I remove <trust level="Full"/> tag from web.config I will not get any error but no display too. A blank page will be shown
Am really confused about this! I am left with no other alternatives except to approach this forum now! Any experts out there who faced this issue or anything I have to configure more on web.config?
Place The below code
<compilation debug="true" targetFramework="4.0">
instead of
<compilation targetFramework="4.0" defaultLanguage="c#"/>
hope it's works
This Link actually helps you to identify any sort of issues if it is not displayed in the server and thus I [OP] was able to fix my problem by identifying the issue.
Below is what the link contains:
When using ASP.NET, you may encounter the following error: "Security
Exception" "Exception Details: System.Security.SecurityException:
Security error."
This means a class during runtime is linked against a restricted
class. By default ASP.NET error messages are optimized for
compile-time and run-time errors only. So the real error sources are
not displayed for linked errors. These errors arise for example, if
you try to access registry variables or system variables etc.
Sometimes you can get detailed error messages by placing the following
code in global.asax file.
<%# Application %>
<script runat="server" language="c#">
protected void Application_Error(Object sender, EventArgs e){
Exception ex = Server.GetLastError();
Server.ClearError();
Response.Write("<pre>");
Response.Write(Environment.NewLine);
Response.Write("Caught Exception: " + ex.ToString() + Environment.NewLine);
if (ex.InnerException != null){
Response.Write(Environment.NewLine);
Response.Write("Inner Exception: " + ex.InnerException.ToString() + Environment.NewLine);
Response.Write(Environment.NewLine);
}
System.Security.SecurityException ex_security = ex as System.Security.SecurityException;
if (ex_security != null){
Response.Write(Environment.NewLine);
Response.Write("Security Exception Details:");
Response.Write(Environment.NewLine);
Response.Write("===========================");
Response.Write(Environment.NewLine);
Response.Write("PermissionState: " + ex_security.PermissionState + Environment.NewLine);
Response.Write("PermissionType: " + ex_security.PermissionType + Environment.NewLine);
Response.Write("RefusedSet: " + ex_security.RefusedSet + Environment.NewLine);
}
Response.Write("</pre>");
}
</script>
If you are using asp.net mvc5, it needs full trust, which is not supported by plesk (host gator) at the moment I write this.
If that is the case you need to downgrade to mvc4 or find another hosting provider which supports full trust

Visual Studio 2013 Razor intellisense not recognizing the start of a lambda expression

I'm having a minor annoyance with VS 2013 when working in ASP.NET MVC Razor views. When typing something like #Html.TextBoxFor(m => m.[PropertyName]), as soon as I type the first m, intellisense pops up a drop down with mbox selected. Then when I press the space key it inserts mbox into my code. Is there anything I can do, besides turning off intellisense, so that I can type my code without having to dismiss the intellisense suggesetion? If I type #Html.TextBoxFor() first, and then go back and fill in the expression, I get the desired behavior, but that is no more convenient.
Edit
I don't have anything imported directly in this view. In the web.configs I have the following:
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
<add namespace="EPiServer.Shell.Web.Mvc.Html" />
<add namespace="EPiServer.Framework.Web.Mvc.Html" />
<add namespace="EPiServer.Web.Mvc.Html" />
<add namespace="LGMVCWeb.ViewModels"/>
<add namespace="LGMVCWeb.Models.Pages.OE"/>
<add namespace="LGMVCWeb.ViewModels.Pages.OE"/>
<add namespace="LGMVCWeb.ViewModels.Shared.OE"/>

How to specify the networkCredential param using xml in Serilog.Sinks.Email

I'm trying to setup this sink Serilog.Sinks.Email using xml
<add key="serilog:write-to:Email.restrictedToMinimumLevel" value="Warning" />
<add key="serilog:write-to:Email.from" value="email-from" />
<add key="serilog:write-to:Email.to" value="email-to" />
<add key="serilog:write-to:Email.mailServer" value="mail-server" />
<add key="serilog:write-to:Email.outputTemplate" value="tmpl" />
<add key="serilog:write-to:Email.networkCredential" value="???" />
since networkCredential implements ICredentialsByHost, is possible to specify it using xml?
This is a limitation in the config for the email sink, and/or appsettings support at the moment.

<IndexUserFields> fields haven't been indexed

I'm working on an Umbraco project.
I'm using a StandardAnalyzer indexer and I've just added an ArabicAnalyzer indexer.
The fields under <IndexUserFields> haven't been indexed.
part of my ExamineIndex.config file:
<IndexSet SetName="ArabicIndexSet" IndexPath="~/App_Data/TEMP/ExamineIndexes/Arabic/" >
<IndexAttributeFields>
<add Name="id" />
<add Name="sortOrder"/>
<add Name="nodeName" EnableSorting="true"/>
</IndexAttributeFields>
<IndexUserFields>
<add Name="_AllContents"/>
<add Name="cityName"/>
<add Name="countryName"/>
</IndexUserFields>
<IncludeNodeTypes>
<add Name="Country" />
<add Name="DestinationInfoTopic" />
<add Name="Article" />
</IncludeNodeTypes>
</IndexSet>
All <IndexAttributeFields> fields are indexed, but <IndexUserFields> aren't, BTW the same fields are indexed using StandardAnalyzer.
Do I need to do something extra in order to index these fields?
After hours of digging finally I found an answer to my problem.
Due to our ArabicAnalyzer usage we must add the indexSet name to the Indexer provider:
<add name="ArabicIndexer"
type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
supportUnpublished="false"
supportProtected="false"
interval="15"
analyzer="Lucene.Net.Analysis.AR.ArabicAnalyzer, Lucene.Net.Contrib.Analyzers"
indexSet="ArabicIndexSet" />

Setting up SimpleMembership in MVC4

I am reading that in MVC4 to set up simple membership I should do this step:
In the AppSettings include a line:
<add key="enableSimpleMembership" value="true" />
However when I look at the samples generated from the templates they only have:
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
So why do I keep reading it's necessary to set the enableSimpleMembership key?
By default the SimpleMembershipProvider is enabled when you create a new ASP.NET MVC 4 application. But some hosting providers might disable it by overriding this setting in a higher level web.config.
Quote from an article about SimpleMembership:
If you see an error that tells you that a property must be an
instance of ExtendedMembershipProvider, the site might not be
configured to use the ASP.NET Web Pages membership system
(SimpleMembership). This can sometimes occur if a hosting provider's
server is configured differently than your local server. To fix this,
add the following element to the site's Web.config file:
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>
This setting is used by the WebMatrix.WebData.PreApplicationStartCode method which executes automatically when your site runs and will use the value of this setting to enable the simple membership provider.
Actually configuring the SimpleMembershipProvider explicitly is what I would recommend you:
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear/>
<add name="SimpleMembershipProvider"
type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear/>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
</providers>
</roleManager>
Now, there's no room for confusion anymore. Both the membership and role providers are configured explicitly.

Resources