Use filters to authenticate logins and login-free

Source: Internet
Author: User

Use filters to authenticate logins and login-free

Build a class Checkingloginattribute inherit ActionFilterAttribute

Rewrite onactionexecuting

Inside Code:

0.0 determine if the action or action controller is affixed with the Skipchecklogin label or verify that you are logged in
Type Skiptype = typeof (Skipcheckloginattribute);
if (filterContext.ActionDescriptor.IsDefined (skiptype,false) | | Filtercontext.actiondescriptor. Controllerdescriptor.isdefined (Skiptype,false))
{
Return
}
var container = checkmanager.getdata<icontainer> (KEYS.AUTOFAC);
1, Judge Session[keys.uinfo] whether there is a value otherwise reminder and jump to login page
if (filtercontext.httpcontext.session[keys.uinfo]==null)
{
2.0 first judge to see if the browser brings cookies to the server.
if (filterContext.HttpContext.Request. Cookies[keys.isremember]!=null)
{
2.01 the value at the same place where the cookie is obtained is the value of the UID indicating a login-free
String userId = Filtercontext.httpcontext.request.cookies[keys.isremember]. Value;
2.02 using UID to isolate entity class objects from SysUserInfo
The first thing to get AUTOAC container object
container = checkmanager.getdata<icontainer> (KEYS.AUTOFAC);
ISYSUSERINFOBLL userinfo = container. Resolve<isysuserinfobll> ();
ISYSUSERINFOBLL userinfo = getinstanceforinterfacebyautofac.getinstance<isysuserinfobll> ();
int uid = Int. Parse (userId);//If there is a problem, you will get an error
int uid = Userid.asint ();
var user = UserInfo. Querywhere (c = c.uid = = UID). FirstOrDefault ();
Judge it.
if (user==null)
{
Tologin (Filtercontext);
}
Else
{
2.03 storing the user object in session
Filtercontext.httpcontext.session[keys.uinfo] = user;
Caches the current user rights data in the
ISYSPERMISSLISTBLL PERBLL = getinstanceforinterfacebyautofac.getinstance<isyspermisslistbll> ();
Perbll. Getpermisslistbycache (USER.UID);
}
}
Else
{
2, indicating that there is no login and no sign-in then jump to the login page (both Ajax asynchronous requests and browser navigation requests)
Tologin (Filtercontext);

}
}
}
<summary>
Encapsulation method
</summary>
<param name= "Filtercontext" ></param>
private void Tologin (ActionExecutingContext filtercontext)
{
1 to determine whether the current request is an AJAX request
if (FilterContext.HttpContext.Request.IsAjaxRequest ())
{
Returns a JSON format because this method requires only a different view to jump before entering the action method
Jsonresult json = new Jsonresult ();
Json. Data = new {status=2,msg= "You are not logged in or the login is invalid! "};
Allow GET requests
Json. Jsonrequestbehavior = Jsonrequestbehavior.allowget;
Filtercontext.result = JSON;
}
Else
{
Return to login view direct content user experience not good in areas admin view shared Riga a view to jump and prompt
ViewResult view = new ViewResult ();
View. ViewName = "/areas/admin/views/shared/nologin.cshtml";
Filtercontext.result = view;
}
}

Add to Global filter

Registered in the filterconfig inside the App_start.

Filters. ADD (New Checkingloginattribute ());//Register login to verify first registered first execution

Paste the custom label on the login page and the action method of the CAPTCHA Skipcheckloginattribute skip login Verification

To build a custom label like this first Skipcheckloginattribute

<summary>
If you paste this tag in a class or action method, it means that login verification is skipped
</summary>
[AttributeUsage (attributetargets.class| AttributeTargets.Method)]
public class Skipcheckloginattribute:attribute
{
}

-------------------------------------------

Impersonation Free Login

stored in memory when you create a AUTOFAC container object because you want to take the entity class object in the back.

Build a dedicated class for read and write caching

//<summary>
//package read/write Cache
//</summary>
public class Checkmanager
{
//<summary& Gt
///Permanent Cache AUTOFAC container when the IIS process restarts, the cache disappears and then executes Application_Start () and caches the AUTOFAC container
//</summary>
//< param name= "key" ></PARAM>
//<param name= "Val" ></PARAM>
public static void SetData ( String Key,object val)
{
//cache with no expiration time
Httpruntime.cache[key] = val;
}
//<summary>
//Get AUTOFAC container in cache generic method based on key or object of the specified type
//</summary>
//<typeparam name= "T" ></TYPEPARAM>
//<param name= "key" ></PARAM>
//<returns></returns>
public static T getdata<t> (string key)
{
return (T) Httpruntime.cache[key];
}

<summary>
Remove Cache
</summary>
<param name= "Key" ></param>
public static void Remove (String key)
{
HttpRuntime.Cache.Remove (key);
}
}

Call the above method to cache the Autofac container class object when creating the Autofac container class object

5.0 creating a AUTOFAC true container
IContainer container = Bulider. Build ();
AOTUFAC container There is a way to verify the admission cookie object with the following
AOTUFAC Container intentionally a method resolve (), only need to provide an excuse name can be in the form of an interface to return the implementation of the sub-class object of the interface
ISYSUSERINFOBLL userinfo= container. Resolve<isysuserinfobll> ();
5.01 storing the container in the cache (global cache) easy to use after removing it
Checkmanager.setdata (KEYS.AUTOFAC, container);
Get AUTOFAC Container Object
Checkmanager.getdata<icontainer> (KEYS.AUTOFAC);

Use filters to authenticate logins and login-free

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.