c# - UserRoles Not Working In Asp.Net -
i trying userroles
work in asp.net. facing issues. reason doesn't seem pick roles db.
i changing table name meet requirement using following code in identitymodels.cs
protected override void onmodelcreating(system.data.entity.dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); modelbuilder.entity<identityuserrole>().totable("myrroles"); }
at first couldn't find schema , after doing research followed this article , particular error disappeared. following article added following tables in db
aspnet_applications aspnet_roles aspnet_schemaversions aspnet_users aspnet_usersinroles
which don't need. i'm doing basic if
statement check whether roles working of not. , doesn't seem pick roles.
if(roles.roleexists("rolename")) { button1.visible = false; }
in web.config
file have following code
<rolemanager defaultprovider="myconn" enabled="true"> <providers> <add name="myconn" type="system.web.security.sqlroleprovider" connectionstringname="myconn" applicationname="myapp"/> </providers> </rolemanager>
can tell me going wrong please. thank , support
if want a basic if statement check whether roles working of not
need store roles somewhere, right? , since want use default role provider need these sql tables. default provider @ system.web.security.sqlroleprovider
uses them. because roles.roleexists("rolename")
search these tables see if current user assigned rolename
role.
if want alter way user assigned role, need implement own role provider.
here example:
public class myroleprovider : roleprovider { public override void adduserstoroles(string[] usernames, string[] rolenames) { // logic here } public override bool isuserinrole(string username, string rolename) { // logic here } public override string[] getallroles() { // logic here } public override bool roleexists(string rolename) { // logic here } // etc... }
check article available methods: http://msdn.microsoft.com/en-us/library/8fw7xh74(v=vs.100).aspx.
then, in order setup application use provider update web.config file:
<rolemanager defaultprovider="myroleprovider" enabled="true" cacherolesincookie="true" cookiename=".asproles" cookietimeout="3" cookiepath="/" cookierequiressl="false" cookieslidingexpiration="true" cookieprotection="all"> <providers> <clear /> <add name="myroleprovider" type="mynamespace.myroleprovider" connectionstringname="defaultconnection" applicationname="myappname" writeexceptionstoeventlog="false" /> </providers> </rolemanager>
of course, need update code own namespace, options, etc...
after that, code @ roles.roleexists("rolename")
execute roleexists
method custom role provider.
Comments
Post a Comment