-
Notifications
You must be signed in to change notification settings - Fork 0
/
.NetTrash
72 lines (56 loc) · 3.39 KB
/
.NetTrash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/** Trash */
1. MVC4 Internet Aplication Simple Membership Provider
2. InitializeSimpleMembershipAttribute in Filters/InitializeSimpleMembershipAttribute.cs
it takes care of initializing the simple membership provider,
specify the context class that should be used to perform the DB operations i.e. UsersContext
it will create the membership database in the location specified in the connection string
It will create the table for our UserProfile model with all the columns for the properties specified in our UserProfile class
It will use the UserId field to maintain the foreign key relationship with the auto generated tables.
It will use the UserName field as the field required for login.
It will then create some of the tables needed for the authentication and authorization
Global.asax file:
One per aplication
Every aplication file can see this class
There is 3 types of events: when page requested, when page gets sent and conditional events
Request event types:
Applcation_BeginRequest: called when new request received
Application_AuthenticateRequest: called when requet is ready for authentication
Application_AuthorizeRequest: called when requet is ready for authorisation
Application_ResolveRequestCache: used by output cache to stop cached requests
Application_PreRequestHandlerExecute: ??
Events when page is sent:
Application_PostRequestHandlerExecte: called when requet completed
Application_ReleaseRequestState: called when request state should be stored
Application_UpdateRequestCache: called when processing is complete & file is ready for cache
Application_EndRequest: called when Web App ends
Conditional events:
Application_Starts: when App start
Application_Ends: when ends icluding things like closing db connection
Session_Start:
Session_Ends:
Application_Error: called when unhandled error occured
!! WebSecurity.InitializeDatabaseConnection method will be used to specify the database
using the connection string("DefaultConnection" in this case),
the model which should be used to take the user profile information("UserProfile" in this case),
the filed that should be used for login("UserName" in this case).
!! AccountController:
this class contains all the CRUD operations on our user profile
The important thing to notice is the InitializeSimpleMembership associated with the controller. So whenever user tries to login/register, the simple membership will get initialize automatically.
[InitializeSimpleMembership]
public class AccountController : Controller
{
}
4. run the application and click on "register".
This will call the AccountController, which will in turn call the InitializeSimpleMembership constructor and will create the simple membership database along with the UserProfile table that we needed for our UserProfile model.
Simple membership provider created a tables:
UserProfile
webpages_Membership
webpages_Roles
webpages_UserInRoles - this table will keep track of the user using the open authentication to login to the application
Connection to MS-SQL
using System.Web.Configuration;
<connectionStrings >
<add name="myConnectionString" connectionString="Server=myServerAddress;Database=myDataBase;
User ID=myUsername;Password=myPassword;Trusted_Connection=False;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Keeps the Global.asax.cs clean from code that should probably be encapsulated the way MVC 4 is over previous versions. Leaves a nice clean: