Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!
By reading my question, you would get the idea.
I'm working on application that requires user login information to access to members-only resources/pages. My question is, when I verified from database that user is valid user, how can I register him in a way that satisfy the following:

Request.IsAuthenticated
'is user is authenticated to view that requested page or not?

I mean, if we create a new project in VS 2010, it creates a database, an account folder( some members only page), when we register using default setting, it automatically signed us in and give access to members only pages.
how can we implement this behavior using my own custom settings?
Any suggestion anything will be very very helpful.
thanks in advance.

(P.S: please ask me before reporting 'bad question' to this question. please!)
Posted

1 solution

You can add a custom Role Provider class inherited from RoleProvider and override the functions. After adding the class, change your default provider name in the web config

For Example:
Add a class named CustomRoleProvider to you Appcode folder

using System;
using System.Collections.Specialized;
using System.Web.Security;

namespace TestApp.AppCode
{
public class CustomRoleProvider : RoleProvider
{

public override string ApplicationName { get; set; }



public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);

}
public override bool IsUserInRole(string username, string roleName)
{
string[] roles = GetRolesForUser(username);
foreach (string role in roles)
{
if (role.Equals(roleName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}

public override string[] GetRolesForUser(string username)
{
//Add code to get user roles
}

public override void AddUsersToRoles(string[] usernames, string[] roleNames){

}

public override void CreateRole(string roleName) { }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { return false; }
public override string[] FindUsersInRole(string roleName, string usernameToMatch) { return null; }
public override string[] GetAllRoles() { return null; }
public override string[] GetUsersInRole(string roleName) { return null; }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { }
public override bool RoleExists(string roleName) { return false; }

}
}
}

Change your web config role provider
XML
<roleManager enabled="true" defaultProvider="CustomRoleProvider" >
     <providers>
       <clear/>
       <add name="CustomRoleProvider" type="TestApp.AppCode.CustomRoleProvider"/>
     </providers>
   </roleManager>
 
Share this answer
 
Comments
Sikander Hayyat 12-Sep-14 17:02pm    
If I say that the application is relatively large application? is this work?
Thanks in advance :)
Abdul Samad KP 12-Sep-14 19:03pm    
If you want to provide user role based access then this will work, I have used this in a couple of projects, but I haven't used membership provider. There is no connection between application size and this. The only thing is, you have to override all methods as per your requirements

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900