... That seems straight forward, but I want to know what its doing - I want to know if that's hows it's supposed to work (is it that easy!?!?) Lets say my usernames and passwords were stored into a local SQL compact DB. Could I use WebSecurity.Login against
that source? Or does the whole authentication model come from the .NET Authentication? Whats best practice here? Erik
WebSecurity is a wrapper around the ASP.NET Membership provider. If you read the tutorial, you should see that you point it at your database and it will generate the membership tables if needed. From that point on, it acts in pretty much the same way as
the more familiar version that's been available since ASP.NET 2.0. It's got a couple of additional natty features such as the ability to generate and send confirmation emails etc, but essentially it's a simplified version of its sibling.
You could also use an existing database with this provider. ASP.NET Web Pages doesn´t use the original SqlMembershipProvider, but instead a new provider called SimpleMembershipProvider.
With this provider you can easily change the tables you want to use for the user and the profile (it can be two different tables).
If you create a new "Starter Page" from the template and open _start.cshtml you can see this:
That line of code creates a new connection to the database "StarterSite.sdf", and set the user table to "UserProfile", userid column to "UserId", username column to "Email" and also a option to create tables automaically to true.
You could change this to whatever you want if you have your existing user database which you want to use.
Mikael Söderström
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
I see that when I used WebSecurity it added (automatically) the "simple membership tables" to my .sdf database. Very cool... However, the WebSecurity.CreateAccount(); does not function correct as I am recieving an error. Exception Details: System.Web.Security.MembershipCreateUserException:
The Provider encountered an unknown error. Anythought?
[MembershipCreateUserException: The Provider encountered an unknown error.]
I'm not sure if proper naming conventions are a big deal
Actually, it is. If you have a file with the name "_start.cshtml", it will be executed the first time you run the application, like Application_Start in global.asax. If you want to execute code before and after a single page loads you can use "_init.cshtml".
Try to change the name to _start.cshtml instead of _config.cshtml.
Current web pages API is really in bad condition. Is there any documentation online, which describes all the functionality of WebSecurity helper? This link
http://www.asp.net/webmatrix/tutorials/asp-net-web-pages-api-reference doesn't even contains a InitializeDatabaseConnection method. I'm asking because I'd need a helper method, which generates a role and sets it to an user...
I just took a look at the helpers using reflection, and it doesn´t seems like it´s possible to add users with the helpers in the beta. As it is now you will have to do that manually, or use the database-helper to do it with code.
Mikael Söderström
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Erik5388
Member
81 Points
84 Posts
Web Matrix Authentication
Jul 15, 2010 06:47 PM|LINK
I see web helpers for authentication methods, can someone show me this in action. If I have page called, "login.cshtml" with the contents of;
<form method="post" action="login.cshtml"> <input name="username" type="text"> <input name="password" type="password"> <input value="Login" type="submit"> </form>and some RAZOR of;
@{ string username = Request.Form["username"]; string password = Request.Form["username"]; if(WebSecurity.Login(username, password)) { Response.Redirect("~/Folder/File"); } else { string error = "Username or password incorrect!"; } }... That seems straight forward, but I want to know what its doing - I want to know if that's hows it's supposed to work (is it that easy!?!?) Lets say my usernames and passwords were stored into a local SQL compact DB. Could I use WebSecurity.Login against that source? Or does the whole authentication model come from the .NET Authentication? Whats best practice here? Erik
webmatrix RAZOR login authentication asp.net SQL Compact
zettersten.com
client side dev
canvas/html5/js/css
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Web Matrix Authentication
Jul 15, 2010 07:41 PM|LINK
WebSecurity is a wrapper around the ASP.NET Membership provider. If you read the tutorial, you should see that you point it at your database and it will generate the membership tables if needed. From that point on, it acts in pretty much the same way as the more familiar version that's been available since ASP.NET 2.0. It's got a couple of additional natty features such as the ability to generate and send confirmation emails etc, but essentially it's a simplified version of its sibling.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Web Matrix Authentication
Jul 15, 2010 11:09 PM|LINK
You could also use an existing database with this provider. ASP.NET Web Pages doesn´t use the original SqlMembershipProvider, but instead a new provider called SimpleMembershipProvider.
With this provider you can easily change the tables you want to use for the user and the profile (it can be two different tables).
If you create a new "Starter Page" from the template and open _start.cshtml you can see this:
WebSecurity.InitializeDatabaseConnection("StarterSite", "UserProfile", "UserId", "Email", true);That line of code creates a new connection to the database "StarterSite.sdf", and set the user table to "UserProfile", userid column to "UserId", username column to "Email" and also a option to create tables automaically to true.
You could change this to whatever you want if you have your existing user database which you want to use.
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Erik5388
Member
81 Points
84 Posts
Re: Web Matrix Authentication
Jul 16, 2010 04:25 PM|LINK
I see that when I used WebSecurity it added (automatically) the "simple membership tables" to my .sdf database. Very cool... However, the WebSecurity.CreateAccount(); does not function correct as I am recieving an error. Exception Details: System.Web.Security.MembershipCreateUserException: The Provider encountered an unknown error. Anythought?
zettersten.com
client side dev
canvas/html5/js/css
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Web Matrix Authentication
Jul 16, 2010 06:15 PM|LINK
I got that error when I tried to create a new instance of the provider without using InitializeDatabaseConnection correctly.
Do you call InitializeDatabaseConnection in your code (for example in _start.cshtml)?
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
Erik5388
Member
81 Points
84 Posts
Re: Web Matrix Authentication
Jul 16, 2010 08:54 PM|LINK
I'm not sure if proper naming conventions are a big deal, but yes i called InitializeDatabaseConnection withing my "_config.cshtml" file.
I passed ... WebSecurity.InitializeDatabaseConnection("StarterSite", "UserProfile", "UserId", "Email", true);
Just like you suggested.
zettersten.com
client side dev
canvas/html5/js/css
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Web Matrix Authentication
Jul 16, 2010 09:16 PM|LINK
Actually, it is. If you have a file with the name "_start.cshtml", it will be executed the first time you run the application, like Application_Start in global.asax. If you want to execute code before and after a single page loads you can use "_init.cshtml".
Try to change the name to _start.cshtml instead of _config.cshtml.
I have blogged about these files here:
http://weblogs.asp.net/mikaelsoderstrom/archive/2010/07/07/run-code-automatically-in-asp-net-web-pages.aspx
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
zigomir
Member
38 Points
35 Posts
Re: Web Matrix Authentication
Jul 20, 2010 09:29 AM|LINK
Current web pages API is really in bad condition. Is there any documentation online, which describes all the functionality of WebSecurity helper? This link http://www.asp.net/webmatrix/tutorials/asp-net-web-pages-api-reference doesn't even contains a InitializeDatabaseConnection method. I'm asking because I'd need a helper method, which generates a role and sets it to an user...
Vimpyboy
Contributor
3212 Points
651 Posts
MVP
Re: Web Matrix Authentication
Jul 20, 2010 09:55 AM|LINK
I just took a look at the helpers using reflection, and it doesn´t seems like it´s possible to add users with the helpers in the beta. As it is now you will have to do that manually, or use the database-helper to do it with code.
http://weblogs.asp.net/mikaelsoderstrom
http://www.twitter.com/vimpyboy
zigomir
Member
38 Points
35 Posts
Re: Web Matrix Authentication
Jul 20, 2010 11:47 AM|LINK
I've managed to solve the problem manually. Tnx!
Hope we'll get more useful helpers in future, so it will be possible to manage roles without SQL sentences... =)