Asp.net mvc 1.0 control page Access Permissions

Source: Internet
Author: User

I came into contact with the asp.net mvc framework two days ago. Thanks to the excellent tutorials in the blog.

During this period of time, the teacher asked me to build a project and thought about how to use the asp.net mvc Framework. He forced himself to learn more, so he put on his head! One such problem exists. The pages under the sysmanage file under my views file only implement a single function .. in this case, you want to assume a permission. the user logs on and obtains the user information! Obtain the permissions of the user and compare whether the user has the permission to access the page!

First, I want to use an interceptor! However, I don't know how to get the action method name. Because the action method name corresponds to the name of the page to be accessed, in this way, we can compare it with the database to determine whether the user has the permission to access the page! I knocked on the code myself. I checked some attribute methods and found out how to get the action method name.

Example:

Reference content is as follows:
Public class VisitFilter: FilterAttribute, IActionFilter
{
/// <Summary>
/// Intercept the action before calling it
/// </Summary>
/// <Param name = "myFilterContext"> </param>
Void IActionFilter. OnActionExecuting (ActionExecutingContext myFilterContext)
{
// Assume that the user's permissions are obtained.
String UserPower = "UserPower ";
If (UserPower! = MyFilterContext. ActionDescriptor. ActionName)
{
Throw new InvalidOperationException (@ "no access permission ");
}
}
Void IActionFilter. OnActionExecuted (ActionExecutedContext myFilterContext)
{
}
}

But the next question is, how can I go to its custom page without permission? Can I transfer an action to another action ?...?
With this question, I kept looking at methods, attributes, and I couldn't find out how to do it. but it seems impossible to think about it. (if any of you know the method, please share it with me. I sincerely thank you ).

The above code throws an exception .. yes, it's just him, because we can also write an exception interceptor. in this way, we can capture the exceptions we throw, and define the error information by ourselves .. when an exception occurs, the system automatically jumps to Views/Shared/Error. aspx. we will display our own error messages on the page to prompt users .. this will achieve the effect .. haha. the exception interceptor code is as follows:

Reference content is as follows:
Public class ExceptionFilter: FilterAttribute, IExceptionFilter
{
/// <Summary>
/// Intercept an exception in action
/// </Summary>
/// <Param name = "myFilterContext"> </param>
Void IExceptionFilter. OnException (ExceptionContext myFilterContext)
{
MyFilterContext. Controller. ViewData ["ErrorMsg"] = myFilterContext. Exception. Message;
MyFilterContext. Result = new ViewResult ()
{
ViewName = "Error ",
ViewData = myFilterContext. Controller. ViewData,
};
MyFilterContext. ExceptionHandled = true;
}
}

Here I modified it directly on the page that comes with the asp.net mvc Framework, and verified the download of the About source code...

Author blog: http://www.cnblogs.com/NetChessman/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.