Configuration Error in mvc5 - c#

I'm trying to add authentication and authorization to my website.I'm ding it with a help of tutorial. I added to web.config file and global.asax file following code as the tutorial does.But it gives me an error when i try to run the application.can anyone explain the error that i'v done..I created project by choosing "NO authentication" and I've installed Microsoft.AspNet.WebHelpers and Microsoft.AspNet.WebPages.Data packages.....I also added the WebMatrix.Data and WebMatrix.WebData referencesto the application
web.config file
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<membership defaultProvider="ePlannerProvider">
<providers>
<clear />
<add name="ePlannerProvider" type="WebMatrix.WebData.ePlannerProvider, WebMatrix.WebData" />
</providers>
</membership>
<roleManager enabled="true"/>
</system.web>
Global.asax file
WebSecurity.InitializeDatabaseConnection("EPlannerDatabaseEntities", "User", "Id", "Email", false);
Error message:
Parser Error Message: Could not load type 'WebMatrix.WebData.ePlannerProvider' from assembly 'WebMatrix.WebData'.
Source Error:
<providers>
<clear />
<add name="ePlannerProvider" type="WebMatrix.WebData.ePlannerProvider, WebMatrix.WebData" />
</providers>
</membership>

Error message says the type is not found.
Try to see version is right and use object browser to see if the class exists.
Also in reference, go to properties and set copy local as true.
Typically class names start with capital character

Related

Enable Role Manager mvc5

I have a MVC 5 Project.
I want to use Roles.GetAllRoles().
But i get the System.Configuration.Provider.Exception.
I tryed to solve it with modifying the Web.Config, but i still get the error.
<appSettings>
<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" />
</appSettings>
<system.web>
<roleManager defaultProvider="CustomRoleProvider">
<providers>
<clear />
<add name="CustomRoleProvider" type="MyProjekt.CustomRoleProvider" />
</providers>
</roleManager>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.1" />
I also tryed to add
<roleManager
enabled="true"
cacheRolesInCookie="true" >
but then i get
An exception of type 'System.Web.HttpException' occurred in
System.Web.dll but was not handled in user code
Additional information: No Connection with SQL Server-Database.
I got caught out by this for a while Roles.IsUserInRole("Admin") which sent me down the route of looking for role providers etc. I should of just been using User.IsInRole("Admin")
the following was a usefull article in enabling roles
http://geekswithblogs.net/MightyZot/archive/2014/12/28/implementing-rolemanager-in-asp.net-mvc-5.aspx
I know this maybe does not pertain specifically to your question, but I just discovered how to find the Role a particular User is assigned to:
Dim userInfo = UserManager.FindById(User.Identity.GetUserId())
Dim userRole As String = userInfo.Roles(0).RoleId
Dim thisRole As String = db.Roles.Where(Function(x) x.Id = userRole).FirstOrDefault().Name
That was bugging me for a while so I just had to get it out there!

Get Windows Logon From Within ASP.NET Page

Just like the title states. Need the windows logon and domain info from within our asp.net page.
I tried
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
but it returns the IIS App Pool not the user name
Thanks
Try HttpContext.User, accessible simply as User from the code behind. It returns both the domain and username, but should be easy enough to trim for your needs. It's worked for me in the past. You can also use this to manage roles in your application, if you need to.
EDIT
Below are the relevant portions of my web.config. I also used aspnet_regsql.exe to setup the tables needed for the role manager in my database. I could then use User.Identity.Name and User.Identity.IsInRole
<connectionStrings>
<clear/>
<add name="SqlRoleManagerConnection"
connectionString="myConnectionString">
</add>
</connectionStrings>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
<roleManager enabled="true" defaultProvider="SqlRoleManager">
<providers>
<clear/>
<add name="SqlRoleManager"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="SqlRoleManagerConnection"
applicationName="myAppName" />
</providers>
</roleManager>
</system.web>
This is my code in VB.net
var strUser = System.Web.HttpContext.Current.User.Identity.Name
so C# must be in the lines of: (not tested)
string strUser = System.Web.HttpContext.Current.User.Identity.Name;
In the Web.Config file
<configuration>
<system.web>
<authentication mode="Windows"/>
<identity impersonate="true" />
</system.web>
</configuration>

LDAP Authentication in C# - Input string was not in a correct format

I'm trying to authenticate users through LDAP in C#. Each time I try to log in, I get the 'Input string was not in a correct format' error.
This is my connection string:
<connectionStrings>
<add name="MyConnectionString" connectionString="LDAP://123.193.111.22:389.local" />
</connectionStrings>
<system.web>
<roleManager enabled="true" />
<membership defaultProvider="MyMembershipProvider"><providers>
<add name="MyMembershipProvider"
connectionStringName="MyConnectionString"
type="System.Web.Security.ActiveDirectoryMembershipProvider,
System.Web, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
connectionUsername="MyUsername"
connectionPassword="MyPassword"
connectionProtection="Secure"
enableSearchMethods="true" />
</providers>
</membership>
<trust level="Full" />
</system.web>
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Admin/Login"
timeout="450" slidingExpiration="false" protection="All"/>
</authentication>
The error seems to be at type="System.Web.Security.ActiveDirectoryMembershipProvider. Any suggestions would be greatly appreciated. Thanks!
I've managed to figure it out. Had to rework the membership provider code and now I'm able to authenticate users.

"The attribute 'connectionStringName' is missing or empty." in Web.config

I'm all of a sudden getting the following error with my Web.config file and I don't understand what it means:
Parser Error Message: The attribute 'connectionStringName' is missing or empty.
Line 24: <providers>
Line 25: <clear />
Line 26: <add name="SMDPortalMembershipProvider" type="SMDPortalMembershipProvider" />
Line 27: </providers>
Line 28: </membership>
Source File: c:\inetpub\wwwroot\web.config Line: 26
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.272
Here's my config file:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="UODOTNET, Version=2.2.5.7444,
Culture=neutral, PublicKeyToken=335F3FBD4BE82339"/>
<add assembly="System.Core, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="Default.aspx" timeout="2880" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
<membership defaultProvider="SMDPortalMembershipProvider">
<providers>
<clear />
<add name="SMDPortalMembershipProvider" type="SMDPortalMembershipProvider" />
</providers>
</membership>
<customErrors mode="Off"/>
<sessionState cookieName="smd_portal_session" timeout="100"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
There is no error when I load the Default.aspx page, but as soon as my code calls upon the Membership class I'm getting the error.
Your membership provider's config section needs a connectionStringName attribute. Without that connectionStringName, it doesn't know what database to look for membership information in.
You need to add the name of one of your the connection strings in the connectionStrings section to the add tag on line 26.
The connectionStringName attribute is required in order for your Web.Config to be valid. You can't use the Membership class without it.

ASP.Net Linq to Entity: The underlying provider failed on Open

I seem to be having a recurring problem. I get it all working and then this happens again a few days later. I havn't done anything to touch the entity framework or the database that is now failing. The instantiation of the Entity edmx is working but I get the following message when trying to execute a query to the database. The way I've solved it is by deleting the databases and clearing out the SQL MGMT Studio and adding them in again and refressing the edmx file.
I have 2 project: DataLayer where my entity framework definition exists, Business Logic and UI. I noticed there are some differences between the web.config between my DataLayer and the UI and don't know if that's a problem. The database was added to the App_Data folder in the DataLayer. The solution keeps adding a copy of the database to the AppData folder in the UI and addes a numberic extension to the filename in the Server Explorer( In the BL the filename is FCGuide.mdf, in the UI is was anmed FCGuide.mdf0 -- don't understand that).
Today I have deleted the database from the project and from the SQL SErver Management Studio and then tried adding it in again without a resolution. I have provided the web.config but not sure what else is needed to help with the troubleshooting. Thanks in advance.
WEB.CONFIG file from the UI:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;" />
</appSettings>
<system.webServer>
<handlers>
<remove name="ChartImageHandler" />
<add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST"
path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>
</system.webServer>
<system.web>
<authorization>
<allow roles="ADMIN" />
<allow roles="MEMBER" />
<allow roles="GUEST" />
<allow roles="RESTAURANT" />
<allow users="admin" />
<allow roles="MEMBER" />
<allow roles="GUEST" />
<allow roles="GUEST" />
<allow roles="ADMIN" />
<allow users="admin" />
</authorization>
<roleManager enabled="true" />
<authentication mode="Forms" />
<httpHandlers>
<add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
validate="false" />
</httpHandlers>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting"
assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</controls>
</pages>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="FCGuideEntities" connectionString="metadata=res://*/FCGuide.csdl|res://*/FCGuide.ssdl|res://*/FCGuide.msl;provider=System.Data.SqlClient;provider connection string="data source=.;attachdbfilename=|DataDirectory|\FCGuide.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Web Config file from the DataLayer:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<connectionStrings>
<add name="FCGuideEntities" connectionString="metadata=res://*/FCGuide.csdl|res://*/FCGuide.ssdl|res://*/FCGuide.msl;provider=System.Data.SqlClient;provider connection string="data source=.;attachdbfilename=|DataDirectory|\FCGuide.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
"The underlying provider failed on Open": this means the connection string didn't work to allow ADO.NET to open the database.
So the provider connection string:
data source=.;attachdbfilename=|DataDirectory|\FCGuide.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework
needs some work. If you create a test page that uses SQL Connection on that string can you open and perform a simple query?
(Ie. strip things down to as few moving parts as possible).

Categories