Asp.net MVC authentication returns the returnurl of the absolute path

Source: Internet
Author: User

For authentication in Asp.net MVC, you only need to mark a [authorization] on the action or controller to be verified. If the user does not log on, the returned actionresult is httpunauthorizedresult.

Public class httpunauthorizedresult: actionresult {

Public override void executeresult (controllercontext context ){
If (context = NULL ){
Throw new argumentnullexception ("context ");
}

// 401 is the HTTP status code for unauthorized access-setting this
// Will cause the active authentication module to execute its default
// Unauthorized Handler
Context. httpcontext. response. statuscode = 401;
}
}

From the source code of httpunauthorizedresult, we can see that the execution of httpunauthorizedresult is very simple, that is, to set the current httpcontext. the response status code is 401. In this way, the authentication module is activated to execute its default unauthorized handler, that is, to jump to the login page. However, the default address for redirecting to the returnurl parameter is relative, this obviously cannot meet my needs when implementing Single Sign-On under different domain names.

The solution is to inherit the authorizeattribute feature and override the onauthorization method.

Public class clientauthorizeattribute: authorizeattribute
{
Public override void onauthorization (authorizationcontext filtercontext)
{
Base. onauthorization (filtercontext );
If (filtercontext. result is httpunauthorizedresult)
{
Filtercontext. Result = new redirectresult (
String. Concat (formsauthentication. loginurl,
"? Returnurl = ",
Filtercontext. httpcontext. server. urlencode (filtercontext. httpcontext. Request. url. absoluteuri )));
}

}
}

When using the clientauthorizeattribute, you can mark the custom clientauthorizeattribute on the action or controller that requires authentication.

For example:

[Handleerror]
[Clientauthorize (roles = "admin")]
Public class admincontroller: Controller
{
//
// Get:/admin/

Public actionresult index ()
{
Return view ();
}
}

 

For authentication in Asp.net MVC, you only need to mark a [authorization] on the action or controller to be verified. If the user does not log on, the returned actionresult is httpunauthorizedresult.

Public class httpunauthorizedresult: actionresult {

Public override void executeresult (controllercontext context ){
If (context = NULL ){
Throw new argumentnullexception ("context ");
}

// 401 is the HTTP status code for unauthorized access-setting this
// Will cause the active authentication module to execute its default
// Unauthorized Handler
Context. httpcontext. response. statuscode = 401;
}
}

From the source code of httpunauthorizedresult, we can see that the execution of httpunauthorizedresult is very simple, that is, to set the current httpcontext. the response status code is 401. In this way, the authentication module is activated to execute its default unauthorized handler, that is, to jump to the login page. However, the default address for redirecting to the returnurl parameter is relative, this obviously cannot meet my needs when implementing Single Sign-On under different domain names.

The solution is to inherit the authorizeattribute feature and override the onauthorization method.

Public class clientauthorizeattribute: authorizeattribute
{
Public override void onauthorization (authorizationcontext filtercontext)
{
Base. onauthorization (filtercontext );
If (filtercontext. result is httpunauthorizedresult)
{
Filtercontext. Result = new redirectresult (
String. Concat (formsauthentication. loginurl,
"? Returnurl = ",
Filtercontext. httpcontext. server. urlencode (filtercontext. httpcontext. Request. url. absoluteuri )));
}

}
}

When using the clientauthorizeattribute, you can mark the custom clientauthorizeattribute on the action or controller that requires authentication.

For example:

[Handleerror]
[Clientauthorize (roles = "admin")]
Public class admincontroller: Controller
{
//
// Get:/admin/

Public actionresult index ()
{
Return view ();
}
}

 

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.