Check UserRoles in UserControl

using System.Security.Principal;

Code :

IPrincipal user = this.Page.User;

if (User.IsInRole(“administrator”))

{

// do anything

}

Một số hàm thường dùng trong ASP Membership Provider

Tóm lượt từ các tutorial tại www.asp.net

Create User:

link gốc : http://www.asp.net/learn/security/tutorial-05-cs.aspx

MembershipCreateStatus createStatus;

MembershipUser newUser = Membership.CreateUser(Username.Text, Password.Text, Email.Text, passwordQuestion, SecurityAnswer.Text, true, out createStatus);

kiểm tra quá trình create user bằng creatStatus.

Kiểm tra quyền của user:

link : http://www.asp.net/learn/security/tutorial-08-cs.aspx

Get a reference to the currently logged on user

MembershipUser currentUser = Membership.GetUser();

Determine the currently logged on user's UserId value

Guid currentUserId = (Guid)currentUser.ProviderUserKey;

Kiểm tra Role của user hiện tại:

bool allow = (currentUser.IsInRole("Role cần kiểm tra"))

Tạm thời copy 2 em thường dùng nhưng cũng thường quên nhất :(

RemoveHTML

using System.Text.RegularExpressions;

  ...

  public static string RemoveHTML(string in_HTML)
    {
      return Regex.Replace(in_HTML, "<(.|\n)*?>", "");
    }

HOW TO: Rewrite URL / Implement URL Rewriting in ASP.NET

Solution

Reference articles:

  • Tip/Trick: Url Rewriting with ASP.NET by Scott Guthrie – Examines four approaches: 1) Use Request.PathInfo Parameters Instead of QueryStrings; 2) Using an HttpModule to Perform URL Rewriting; 3) Using an HttpModule to Perform Extension-Less URL Rewriting with IIS7; 4) ISAPIRewrite to enable Extension-less URL Rewriting for IIS5 and IIS6; also discusses how to handle ASP.NET postbacks correctly with URL rewriting.
  • URL Rewriting by Salman (CSharpFriends) – simple implementation of URL rewriting logic within the Application_BeginRequest() method of the Global.asax file.
  • Rewrite.NET – A URL Rewriting Engine for .NET by Robert Chartier (15Seconds.com). Solution steps:
    • Create the HttpModule to process the web request and rewrite the URL
    • Add the handler in Web.config
    • Create a configuration section in Web.config to define URL mapping rules
    • Add extensibility by creating a rules engine interface
    • Write class or classes to implement rules engine interface
    • Add code to the HttpModule to dynamically load the desired rules from Web.config
  • URL Rewriting in ASP.NET by Scott Mitchell (MSDN) – examines how to implement URL rewriting in a HTTP module; also explains how to handle postbacks.
  • URL Rewriting with ASP.NET by Richard Birkby (CodeProject) – shows how legacy ASP sites can be upgraded to ASP.NET, while maintaining links from search engines. Solution steps:
    • Create the configuration section in Web.config for defining URL mapping rules
    • Write the configuration section handler class, incorporating the URL rewriting logic
    • Create a call to handler in Global.asax in Application_BeginRequest() method
    • Compile the code and install the rewriter assembly in the Global Assembly Cache (GAC)
    • Configure IIS to map non- .aspx files to the ASP.NET ISAPI extension

Related Resources