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 ();
}
}