c# - MVC Authentication - Easiest Way -


i have looked @ asp.net identity , looks complex , difficult follow. want know easiest way authorize user on login [authorize] data annotation allow them through.

follow these steps:

install following nuget packages

  • microsoft.owin
  • microsoft.owin.host.systemweb
  • microsoft.owin.security
  • microsoft.owin.security.cookies

inside app_start folder, add authconfig this:

public static class authconfig {     public const string defaultauthtype = "defaultappcookie"; //example     public const string loginpath = "system/signin"; //example      public static void configureauth(iappbuilder app)     {         app.usecookieauthentication(new cookieauthenticationoptions         {             authenticationtype = defaultauthtype,             loginpath = new pathstring(loginpath)         });     } } 

in root path of project, add startup.cs this

[assembly: owinstartup(typeof(yourporject.startup))] namespace yourporject {     public class startup     {         public void configuration(iappbuilder app)         {             authconfig.configureauth(app);         }     }  } 

to authenticate user (usually inside login action):

//user = user loggin on, retrieved database  list<claim> claims = new list<claim>             {                 new claim(claimtypes.name, user.name),                 new claim(claimtypes.email, user.email),                 //some other claims             };  claimsidentity identity = new claimsidentity(claims, authconfig.defaultauthtype); iauthenticationmanager authmanager = request.getowincontext().authentication; authmanager.signin(new authenticationproperties() { ispersistent = ispersistent }, identity); 

you need add claimtypes.role authorize specific roles.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -