c# - How to populate user(Identity) roles for a Web application when roles are stored in a SQL Server database -
i have c# based asp.net application form based authentication , needs authorization.
here simplified version of user table (sql server)
uid uname passwordhash userroles ---------------------------------------------- 1 gergergeger proivder;data entry 2 b wergtwetwtw helpdsk; usernamager ... ... i'm quite familiar authentication part. authorization not sure best way:
i know once user authorized, can use identity object his/her info.
the question choice read logged in user's roles on every page other call db table every time them?
i not sure sql server question. asp.net question.
asp.net forms authentication allows application define "principal" (among other things) contains array of strings known "roles." can populate roles db 1 time (when user signs on) serialize principal forms authentication ticket, becomes encrypted cookie on browser. asp.net decodes cookie each http request , provides asp.net c# code via httpcontext.user. can retrieve roles context , never needs talk db again.
storing roles this:
string roles = "admin,member"; formsauthenticationticket authticket = new formsauthenticationticket( 1, userid, //user id datetime.now, datetime.now.addminutes(20), // expiry false, //do not remember roles, "/"); httpcookie cookie = new httpcookie(formsauthentication.formscookiename, formsauthentication.encrypt(authticket)); response.cookies.add(cookie);
Comments
Post a Comment