Asked by:
How to get a simple website working?

Question
-
User1841825477 posted
I have spent much of the last ten days trying to get a simple ASP.NET 4.5.2 website working.
I'm using Visual Studio 2015 Community edition and SQL Server 2016 express.
My start point is the site generated by File>New>Website> ASP.NET Web Forms website.I have been able to create new .aspx pages, link various page controls to stored procedures in SQL Server and add items to the menu.
But I don't understand how to get the security features to work.
The Login and Register pages appear when I click the appropriate menu item - so far so good.What I'm now looking for are answers to the following questions:
Where does the data from the "Register" process go?
How can I manage the database of new users? (Where is the database of registered users?)
How can I assign roles to users?
How can I hide selected pages from the publically visible website?
How can I reveal sets of pages to the different roles that I want to choose and set up?
Thanks
KenThursday, February 8, 2018 5:19 PM
All replies
-
User283571144 posted
Hi farm boy,
Where does the data from the "Register" process go?
As far as I know the default website use the identity to manage the user account.
Details about identity in asp.net, you could refer to below article.
Identity use EF to CRUD the data into database.
Microsoft has create many method, you could use UserManager.Create method to create the new user.
It will use EF to create the new user into database.
Like this code:
protected void CreateUser_Click(object sender, EventArgs e) { var manager = new UserManager(); var user = new ApplicationUser() { UserName = UserName.Text }; IdentityResult result = manager.Create(user, Password.Text); if (result.Succeeded) { IdentityHelper.SignIn(manager, user, isPersistent: false); IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
How can I manage the database of new users? (Where is the database of registered users?)
You could find the database connection string in the web.config file's connectionStrings tag.
Like this:
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-WebSite3-ac6bb7de-76cb-42ab-92ed-eb49f3ccbbe6;AttachDbFilename=|DataDirectory|\aspnet-WebSite3-ac6bb7de-76cb-42ab-92ed-eb49f3ccbbe6.mdf;Integrated Security=SSPI" providerName="System.Data.SqlClient"/> </connectionStrings>
You could manager your database in the VS SQL Server Object View(View---> SQL Server Object View.
How can I assign roles to users?As far as I know, you could use Usermanager.AddtoRole method to achieve your requirement.
More details about how to assign roles to users, you could refer to below article.
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/membership-and-administration
How can I hide selected pages from the publically visible website?
How can I reveal sets of pages to the different roles that I want to choose and set up?
Normally, we could check the user is in role in the master page's page load event.
If user not in the role, you could hidde the page by using below codes:
protected void Page_Load(object sender, EventArgs e) { if (HttpContext.Current.User.IsInRole("canEdit")) {
//adminLink is the a tag in the client html adminLink.Visible = true; } }Best Regards,
Brando
Friday, February 9, 2018 6:15 AM -
User1841825477 posted
Hi Brando,
Thanks for your suggestions.
After another day's investigation I can now see the EF schema that is automatically generated when "Register" is used for the first time.
I accessed the EF data and ran some SQL against the data in the EF schema.
That helped me to get a better understanding of what is meant by the term "Local DB"The "introduction to aspnet identity" article that you suggested is too general and the article context is MVC - which is not the same as the Forms based solution that I want to create. Lastly, I don't know where to add the suggested C# code fragments to my basic template site.
You suggested using UserManager.Create and Usermanager.AddtoRole. Please let me know where to add the C# code fragments to my basic template site.As you also suggested by implication, I installed the WingTipToys solution but I can't get it to work - it assumes SQL Server 2012 express and several other things that I don't understand. I spent a couple of hours trying to fix the problems but it is a much too complex overkill approach to solving the problems that I want to solve.
So let's go back to the drawing board (again) and try to solve one small problem at a time.
Starting with the sample code that VS2015 generates from: (File>New>Website>ASP.NET Forms Website;What do I have to do to make new pages: only visible to users who are logged in?
i.e. The users are already in the AspNetUsers table.It would also help if you could tell me how to generate a schema diagram from the default EF database so that I can see the database structure. (Or is the EF database just a set of independent tables?)
ThanksKen
Saturday, February 10, 2018 2:03 PM -
User475983607 posted
I suggest that you create a new Web Forms with the Individual Account option if not done so already. Use this project as a guide.
farm boy
So let's go back to the drawing board (again) and try to solve one small problem at a time.
Starting with the sample code that VS2015 generates from: (File>New>Website>ASP.NET Forms Website;Web Forms uses the web.config to secure pages within a folder. Once you create the template project suggested above, expand the Account folder in solution explorer and open the web.config.
<?xml version="1.0"?> <configuration> <location path="Manage.aspx"> <system.web> <authorization> <deny users="?"/> </authorization> </system.web> </location> </configuration>
This configuration denies access to anonymous accounts. Accounts that have not authenticated. This configuration convention is very mature in ASP.NET with a lot of docs.
https://msdn.microsoft.com/en-us/library/8d82143t(v=vs.85).aspx
Identity (formerly the ASP Membership Provider) is not required for authorization to work. Identity is nothing more than an API for managing user accounts. The ASP team built Identity so you don't have to. However, the level of abstraction can make understanding what's going on in Identity a bit difficult. There is a piece to Identity (the SignInManager) which creates an authentication cookie after a successful login; Forms Authentication. This cookie is what drives authorization to secured resources. In the newer ASP Core it is called Cookie Authentication.
This tutorial shows the ins and outs of Forms Authentication and how a Principal is created. The linked docs below has the fundamentals that you need to know. See the other docs within the Secuirty section too. You might want to start with the Security Basics.
farm boy
It would also help if you could tell me how to generate a schema diagram from the default EF database so that I can see the database structure. (Or is the EF database just a set of independent tables?)You can do this yourself in SSMS (SQL Server Management Studio). There is a diagram feature where you can create a graphic diagram.
Saturday, February 10, 2018 4:21 PM -
User1841825477 posted
Thanks, I have a few questions:
1: I don't know what you mean by "Individual Account option".
In VS2015 I just use: File>New>Website>ASP.NET Forms Website
I can't see anything that says|: "Individual Account option."2: The auto generated site web-config has 112 lines.
There are four instances of the following text:<!--ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template -->
The link looks like a sales pitch for ASP.NET Identity - complex and not very helpful.
2A: The web.config in the Account section looks just like the code that you posted.
I have read a lot about this -= for example this blog gives some good examples.
I'll look at the links and see where they take me.Identity (formerly the ASP Membership Provider) is not required for authorization to work.
That is good news. So which bits of the auto-generated website do I have to get rid of.
OR:
How do I use VS2015 to generate a website that does not have all of the "ASP.NET Identity" stuff in it.3: SSMS
I have been using SSMS for more than ten years so I know how to create diagrams in SSMS.
But I have encountered two problems:
3.1: In the EF "default" database, the diagramming feature is not on the list of options that I see when I right click on the DB name.
https://1drv.ms/i/s!AkeiYlM3TByIsiy570bosRYwsqMn3.2: I don't know how to move this EF artefact into SQL Server 2016 where I can use SSMS to study it.
I would much prefer to use SQL Server 2016 rather than an intermediate EF artefact to manage authentication and role management - I just don't know how to do it from VS2015.
Note -I have installed the "old" aspnetdb in SQL Sever 2016 but I don't know how to use it with a VS2015 generated website.I do hope that you have some practical answers to my problems.
Thanks
Ken
Saturday, February 10, 2018 8:16 PM -
User475983607 posted
farm boy
1: I don't know what you mean by "Individual Account option".
In VS2015 I just use: File>New>Website>ASP.NET Forms Website
I can't see anything that says|: "Individual Account option."Sorry, I was not specific enough. There is a "Change Authentication" button on the "Select a Template" window that opens model where you can select the "Individual Accounts" option. Make sure you are targeting .NET framework 4.5+ on the Add New Project Windows. (prior screen)
farm boy
That is good news. So which bits of the auto-generated website do I have to get rid of.
OR:
How do I use VS2015 to generate a website that does not have all of the "ASP.NET Identity" stuff in it.See the previous reply and linked tutorial. The tutorial shows how to add email confirmation to the defualt ASP Identity template. This is a good way to become familiar working with the ASP Identity API.
farm boy
3: SSMS
I have been using SSMS for more than ten years so I know how to create diagrams in SSMS.
But I have encountered two problems:
3.1: In the EF "default" database, the diagramming feature is not on the list of options that I see when I right click on the DB name.
https://1drv.ms/i/s!AkeiYlM3TByIsiy570bosRYwsqMnThe linked image is from Visual Studio not SSMS. SSMS is available for download at the following link.
https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms
farm boy
3.2: I don't know how to move this EF artefact into SQL Server 2016 where I can use SSMS to study it.This is an entirely differnet discussion realted to deploying databases. These days I use code first migrations in the old days (not too old) I used deployment scripts. Anyway, for now, the easiest path is to create the Web Forms template with the individual account option (above). Immediately open the web,config and change the connection string to the 2016 instance and database you want. The migration (tempalte) will create the database if the database does not exist. By default the tempate creates the database in (localdb). Finally, run the app and create an account to invoke the migrations (setup the database).
Example connection string.
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\SqlInstanceName;AttachDbFilename=|DataDirectory|\TestDb.mdf;Initial Catalog=TestDb;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
Use the Visual Studio SQL Server Object Explorer to find the local database instance name if you're not sure.
farm boy
Note -I have installed the "old" aspnetdb in SQL Sever 2016 but I don't know how to use it with a VS2015 generated website.ASP Identity does not work with the old aspnetdb tables as the schemas are different. It is possible to make it work by but not practical. The Identity API has new features, like claims, that are common in modern web apps.
Other bits you need to be aware of... The template uses a framework called OWIN (Open Web Interface for .NET) to configure ASP Identity. The framework's goal is to decouple the host and application. You'll see OWIN used a lot in the source code.
With that being said, OWIN has been replaced with ASP Core. Also Web Forms does not exist in ASP Core. Since you are embarking on learning new technology, this might be a good time to investigate MVC (or Razor Pages) in ASP Core.
Sunday, February 11, 2018 1:04 PM -
User1841825477 posted
Thanks for the info but I still can't see the "Individual Account option" (I'm using VS2015)
I have been using Web forms but I also checked Web Application and neither of them look anything like the example in the link that you kindly provided.
I'm wondering Microsoft removed the "Individual Account option" as part of their "improvement" program to upgrade VS2013 to VS2015.
Thanks
Ken
Sunday, February 11, 2018 2:12 PM -
User475983607 posted
Thanks for the info but I still can't see the "Individual Account option" (I'm using VS2015)
I have been using Web forms but I also checked Web Application and neither of them look anything like the example in the link that you kindly provided.
I'm wondering Microsoft removed the "Individual Account option" as part of their "improvement" program to upgrade VS2013 to VS2015.
Thanks
Ken
File -> new Project. Not file -> new Web Site.
See the linked tutorial. While new Web Site creates an Identity template, most examples and tutorials use web projects. Web Sites are not used much in the moden ASP dev so you might want to make the shift to projects now.
Anyway, once you get your bearing you can always go back to a Web Site project and should be able to move around rather easily. The main difference is where the source code is located within the project but the API is the same. The Identity setup is in the App_Code folder, IdentityModels.cs file if you want to take a look. However, the web.config connection string suggestions above will work in a Web App.
Sunday, February 11, 2018 2:48 PM -
User1841825477 posted
I don't have a problem with using a web project.
The point I was trying to make is that VS 2015 does not have an "Individual Account option" (In File>New> Website OR in File> New> Project)
File>New>Project shoes these options:ASP.NET Web Application (.NET Framework): Visual C#
ASP.NET Core Web Application (.NET Core): Visual C#
ASP.NET Core Web Application (.NET Framework): Visual C#The right hand panel contains this text:
Type: Visual C#
Project templates for creating ASP.NET applications.
You can create ASP.NET Web Forms, MVC, or Web API applications
and add many other features in ASP.NET.Application Insights
Add Application Insights to project Optimize performance and monitor usage in your live application.
--------------There is no sign of "Individual Account option" or any of the other controls that are shown in your example.
Ken
Sunday, February 11, 2018 3:22 PM -
User475983607 posted
I don't have a problem with using a web project.
The point I was trying to make is that VS 2015 does not have an "Individual Account option" (In File>New> Website OR in File> New> Project)
File>New>Project shoes these options:ASP.NET Web Application (.NET Framework): Visual C#
ASP.NET Core Web Application (.NET Core): Visual C#
ASP.NET Core Web Application (.NET Framework): Visual C#The right hand panel contains this text:
Type: Visual C#
Project templates for creating ASP.NET applications.
You can create ASP.NET Web Forms, MVC, or Web API applications
and add many other features in ASP.NET.Application Insights
Add Application Insights to project Optimize performance and monitor usage in your live application.
--------------There is no sign of "Individual Account option" or any of the other controls that are shown in your example.
Ken
Yeah, you did not go far enough. It seems you have not read the linked doc either? The first screen is asking you to pick the flavor of project. ASP Core is the new framework/platform. The target framework (at the top) is also an option. Select 4.5 or higher like 4.6.1 if available. I assume you'll want ASP.NET Web Application (.NET Framework): Visual C#. This is the now classic ASP application template that you have been using up to this point. Web sites do not have an ASP Core option and the reason why the project wizard is a bit different for web projects.
The next screen is where you can select the project options.
Sunday, February 11, 2018 4:21 PM -
User1841825477 posted
Yeah, you did not go far enough.
Yes - you are absolutely right! I have now taken the next step and I can see the "Individual Account option".
I'll have another go.
Sorry for the dumb stuff.Ken
Sunday, February 11, 2018 5:34 PM