Monday, February 23, 2009

How To Check User Logged In

To check whether the user has logged in or not a simple 2 lines to be checked in our application


using System.Security.Principal //namespace to be used

IPrincipal principal = HttpContext.Current.User; //gives the current user logged in
return principal.Identity.IsAuthenticated; //isauthenticated gives return true or false

so if user has logged in then it returns true and vice versa , the userid of the logged in user can be retrieved in the below manner


userId = new Guid(Membership.GetUser(Page.User.Identity.Name).ProviderUserKey.ToString());



Thursday, February 12, 2009

Easy Way Of Maintaing The Users and Membership

The easy way of creating new users and settings roles to the existing users is that we can see one icon in the solution explorer named "ASP.NET Configuration"

By clicking this icon we get a new window opened in which we have three links present name
1)Security , 2)Application Configuration , 3)Provider Configuration - with the existing data if any

Now in these threee links click security link for creating users,roles and maintaining users, for creating new roles click Create or Manage roles now we get one textbox and a button where we should type the role we want in our application like Admin,User etc.. your own role names you can write.
After creating roles go for creating users by clicking Create User we get another page in which we have to fill in the details asked and set the role for that user and click "Create User".Do Remember the password because while logging in the website it is important.
If you want to edit the deatils or change the role of any user then click the link name Manage User ,there we can edit any detail of that user.

After creating the roles and users and also assigning the roles to the particular users we can close that window and try logging in the website you have developed.

How to Set A Default Role While Registration Itself

  • This will be possible only if we have aspnet tables and stored procedures in our database.
  • We should have users,roles in our application.
  • Now in our visual studio we will have a register page from which a new user will get registered in our website.
  • In this Register Page,register click we should include one line given below based on your requirement
  • Roles.AddUserInRole("String username","String rolename"); for single username adding ,if we want for a group of users then we can use the method named Roles.AddUsersInRole("String[] usersname","String rolename"); By this the total group given in the string array will be assigned the same role.
  • Here Roles is the aspnet class and AddUserInRole(),AddUsersInRoles() are methodes in that.
  • So with this line we can easily assign the role which we want the registring user to be assigned.