3/05/2011

How to write your custom role provider in ASP.NET : Role provider for oracle

Did you ever have to get the user membership information / Role information from a custom table in a database other than ASPNET SQL db ... Something like you want to get the information about user role from a userinfo procedure !!

There are two primary reasons that might make you look at creating a custom role provider.

* You need to store role information in a data source that is not supported by the role providers included with the .NET Framework, such as a teradata database, an Oracle database etc...

* You need to manage role information using a database schema that is different from the database schema used by the providers that ship with the .NET Framework. E,g May be your company uses a custom schema for role authorization..

So how difficult is it to write your own custom role provider in ASP.NET ??

Believe me its as easier as drinking a glass of water :)....A Custom RoleProvider inherits from the abstract base class RoleProvider and has a number of optional methods and properties that can be overrriden.

Follow the steps below to create your own custom role provider !!

1. Add a new class to your project and name it as Something like "CustomRoleProvider".
2. Make sure you inherit the class from Abstract class Role Provider.


public class CustomRoleProvider : RoleProvider
{
///
/// This method will override the getrolesforuser method in roleprovider and do the custom implementation
///

///
///
public override string[] GetRolesForUser(string username)
{

List<string> roles = new List<string>();
//Write your own implementation and add roles returned by your methond to role array
return roles.ToArray();
}

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

{
throw new NotImplementedException();
}

}

You can choose the methods for which you want to implement in the role provide class. Now your Custom role provider class is ready. Now you need to modify your web.config.

3, You need to enable the custom role provider and let your application know that the Custom role provider is its default role provider. So please add the following section to your web.config in <system.web> section.


<roleManager enabled="true" defaultProvider="RapidRoleProvider" cacheRolesInCookie="true"
cookieName="AppRoles"
cookieTimeout="20"
cookiePath="/"
cookieRequireSSL="false"
cookieSlidingExpiration="true"
cookieProtection="All">

<providers>
<clear/>
<add name="RapidRoleProvider" type="CustomeRoleProvider"/>
providers>
roleManager>

If you are using namespace in your web application, make sure you give full directive of the class location in type ... Namespace.ClassName ....

You can see that we are caching the roles in a protected cookie which expires periodically. Once you set Cacherolesincookie = true , your role provider will not reach your datbase everytime user requests for a resource. Instead it checks in the cookie and fetches the result fastly. The advantage is the improvement in performance of the application.

4. Now you are done with your custom role provider and you are ready to use it.. Lets make a small test using declarative security attributes. Lets take some page which is only accessible to user role "Normal".

[PrincipalPermission(SecurityAction.Demand, Role = "Normal")]

public partial class Default : System.Web.UI.Page

{

// ...

}


When a user tries to access the above page, the GetRolesForUser Method in CustomRoleProvider will be called to verify the user is assigned to the “Normal” Role. If so, the page works fine. If not, a SecurityException will be thrown, not allowing the page to be viewed.

Now your role provider works fine.So you can enable security trimming on your sitemap and implement role based security in your application.

If you have any doubts / Questions, please feel free to leave your comment.

Need more asp.net tips and tricks ? For more Subscribe here or click here to get updates via email

EESJ9D5YMRCC


12 comments:

I want to read more about it, I want to to read a little bit more about , programing files are so good.If blog author can reply for someone's comment follow his comment, I think it is really cool.

ASP.NET comes with a few built-in providers, like SqlRoleProvider, but it is just as easy to role your own.

It took me days before I mastered this CustomRoleProvide. I watched several videos and read different blogs to learn this thing. I'm glad you made a tutorial post on this one.

Nhan sam la mot trong nhung loai thao duoc tu nhien, co mat o rat nhieu quoc gia tren the gioi nhu Nhat Ban, Han Quoc, Hoa Ki... Trong cac loai gia hong sam han quoc ke tren thi nhan sam han quoc luon duoc hang trieu nguoi tren khap the gioi san lung va tim kiem boi nhung gia tri tuyet voi.

nuoc hong sam là một trong những loại thảo dược tự nhiên, có mặt ở rất nhiều quốc gia trên thế giới như Nhật Bản nước hồng sâm, Hàn Quốc, Hoa Kì... Trong các loại nhân sâm kể trên thì nhân sâm hàn quốc luôn được hàng triệu người trên khắp nước hồng sâm hàn quốc thế giới săn lùng và tìm kiếm bởi những giá trị tuyệt vời. Từ xưa đến nay những nuoc hong sam han quoc công dụng của loại nhân sâm này vẫn được mọi người lưu truyền và ứng dụng vào trong đời sống đặc biệt là chăm sóc sức khỏe của con người nước sâm hàn quốc. Củ nhân sâm Hàn Quốc chính là nhân sâm Hàn Quốc ở dạng tươi chưa qua sơ chế thành các sản phẩm. nuoc sam han quoc có những vai trò và tác dụng vô cùng to lớn đối với con người.

sam cu kho là một trong những loại thảo dược tự nhiên, có mặt ở rất nhiều quốc gia trên thế giới như Nhật Bản sâm củ khô, Hàn Quốc, Hoa Kì... Trong các loại nhân sâm kể trên thì nhân sâm hàn quốc luôn được hàng triệu sam cu kho hop thiec người trên khắp thế giới săn lùng và tìm kiếm bởi những giá trị tuyệt vời. Từ xưa sâm củ khô hop thiec đến nay những công dụng của loại nhân sâm này vẫn được mọi người lưu truyền và ứng dụng hong sam cu kho vào trong đời sống đặc biệt là chăm sóc sức khỏe của con người hồng sâm củ khô. Củ nhân sâm Hàn Quốc chính là nhân sâm Hàn Quốc ở dạng tươi chưa qua sơ chế thành hong sam cu kho hop thiec các sản phẩm. hồng sâm củ khô hop thiec có những vai trò và tác dụng vô cùng to lớn đối với con người.

GenericViagraSafe supply best quality generic medicine. All generic medicines available in GenericViagraSafe.com are basically top quality and all medicines are authorized by FDA.

Thanks for sharing valuable information. Your blogs were helpful to Teradata learners. I request to update the blog through step-by-step. Also, find the Teradata news at
Teradata dba Online Training Hyderabad

Another alternative you can utilize is to test remarketing efforts however you should then screen buy volume from rehash buys and ensure that remarketing advertisement is driving a steady volume once it got propelled.

Nhân sâm tươi: Không nên ăn hải sản cùng với nhân  sâm:  Nhân sâm và thực phẩm quý giá và đắt đỏ, bổ ích cho sức khỏe mọi người còn hải sản là món ăn bổ dưỡng, cung cấp nhiều chất dinh dưỡng cho cơ thể. Hải sản và nhân sâm đều là thức ăn có ít cho cơ thể, tuy nhiên hai món này dùng chung lại có hại cho cơ thể. Bởi vì nhân sâm là loại dược liệu có tính âm, hải sản thì là thực phẩm có tính hàn. Với đặc tính trái cực mạnh như vậy sẽ triệt tiêu nhau, vì thể không nên dùng hải sản với nhân sâm để tránh những tổn hại đến sức khoẻ. Vì vậy, hãy nhớ khi ăn nhân sâm đừng ăn cùng hải sản, hoặc ăn trước hay khi ăn hải sản.
Nguồn: ** https://samyennhatminh.com/nhan-sam-tuoi-han-quoc

Post a Comment