ورود

View Full Version : مشکل با IsUserInRole و IsInRole



nima_dir
سه شنبه 29 بهمن 1387, 13:40 عصر
سلام دوستان
فرض کنین دو تا رل داریم و چند تا یوزر که به اینا اساین شدن.من میخوام بر اساسه رلشون بعد از لاگین برن به سه صفحه خاص من تو ایونت LoggedIn کدهای زیر رو امتحان کردم (البته آزمایشیه):

کد:


if (Roles.IsUserInRole("Manager"))
Response.Redirect("Default3.aspx");

و
کد:


if (HttpContext.Current.User.IsInRole("Manager"))
Response.Redirect("Default3.aspx");

که هیچکدومش کار نمیکنه.نمیدونم چرا.جایی رو دارم اشتباه میکنم.این ایونت دقیقا ایونتیه که بعد از لاگین شدن ریز میشه اما نمیدونم چرا به رل دسترسی نداره.ممنون میشم من رو راهنمایی کنین

Saber_Fatholahi
سه شنبه 29 بهمن 1387, 18:44 عصر
سلام
دوست من در مورد کد اولت دقیقا نمی دونم اما واسه کد دومت کاملا درسته و منم توی پروزه هام از همین روش استفاده می کنم و کار میکنه نام نقش هاتو چک کن ببین داری یا نه

nima_dir
چهارشنبه 30 بهمن 1387, 00:36 صبح
سلام
این کدهایی هست که من نوشتم.از رل هم مطمئنم.والله نمیدونم کجا رو دارم اشتباه میرم.چون لاگین انجام میشه ولی کدی که نوشتم اجرا نمیشه:


protected void Login1_LoggedIn(object sender, EventArgs e)
{
if (Roles.IsUserInRole("Manager "))
Response.Redirect("Default3.aspx");
if (HttpContext.Current.User.IsInRole("Manager "))
Response.Redirect("Default3.aspx");
}



<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Default2.aspx" OnLoggedIn="Login1_LoggedIn">
</asp:Login>



<membership>
<providers>
<remove name="AspNetSqlMembershipProvider"/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>

ممنون از لطف شما

nima_dir
چهارشنبه 30 بهمن 1387, 01:11 صبح
من حتی این راه حلها رو هم امتحان کردم اما نشد:
http://66.129.67.4/t/1170772.aspx
http://www.codeverge.net/ng.asp-net-forum.security/login-control-redirect-user-to-specific-page-based-on-roles

jaza_sa
جمعه 02 اسفند 1387, 02:34 صبح
آیا این رو هم امتحان کردی :

Context.User.IsInRole("Manager")

***UNIQUE***
دوشنبه 12 اسفند 1387, 08:40 صبح
سلام
در ادامه همین تاپیک ، من هم همه ی این کارها رو کردم اما باز هم درست نشد، کاربر من میتونه لاگین کنه و تو صفحه اول سایتم اسم کاربر لاگین شده رو نشون میده اما توی ایونت LoggedIn مقدار Context.User.Identity.NAme = "" نمایش داده میشه و ISAuthonticated هم false است (روی نت هم سرچ کردم چند جا نوشته بود که باید اول مقدار True بودن IsAuthonticated بررسی بشه و بعد از خاصیت InRole استفاده بشه)
لفا" راهنمایی کنید مشکل از کجاست
محتویات Web.config من هم به این صورت است


<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Data Source=sql2.moallem.org,1433;Integrated Security=false;Initial Catalog=testformoallem;User ID=MyID;Password=MyPass"
providerName="System.Data.SqlClient" />
<add name="testformoallemConnectionString" connectionString="Data Source=sql2.moallem.org,1433;Integrated Security=false;Initial Catalog=testformoallem;User ID=MyID;Password=MyPass"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings/>

<system.web>

<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->


<roleManager enabled="true" defaultProvider="CustomizedRoleProvider">
<providers>
<add connectionStringName="LocalSqlServer" name="CustomizedRoleProvider"
type="System.Web.Security.SqlRoleProvider" />
</providers>
</roleManager>

<httpRuntime maxRequestLength="1550000" />
<!--executionTimeout="60" maxRequestLength="1550000" requestLengthDiskThreshold="512"/>-->

<!--
Set Sessions to cookieless if you need to retrieve session data
in the upload handler l(configure below in the httphandlers section
and defined in App_Code/Upload.cs)
-->
<sessionState cookieless="UseCookies" />

<!--
cookieless="useuri"
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->


<httpHandlers>
<add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
validate="false" />


</httpHandlers>
<customErrors mode="Off"/>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</buildProviders>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Forms" />

<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<membership defaultProvider="CustomizedProvider">
<providers>
<add connectionStringName="LocalSqlServer" minRequiredPasswordLength="5"
minRequiredNonalphanumericCharacters="0" name="CustomizedProvider"
type="System.Web.Security.SqlMembershipProvider" applicationName="/moallemproject87-01-17" />
</providers>
</membership>

</system.web>






<system.net>
<mailSettings>
<smtp from="noreply@moallem.org">
<network host="mail.moallem.org" password="" userName="" />
</smtp>

</mailSettings>

</system.net>

<!--
settings to allow anonymous users to see the login page.
-->
</configuration>

***UNIQUE***
دوشنبه 12 اسفند 1387, 12:05 عصر
هیچ کس نمیتونه راهنمایی کنه؟!!!!!
من یه دستور ساده هم که توی Event LoggedIn مینویسم کار نمیکنه و انگار اصلا"User رو نمیشناسه که بخواد Role رو تشخیص بده

fakhravari
سه شنبه 03 آبان 1390, 23:59 عصر
با سلام
من از قسمت onloggedin="Login1_LoggedIn" خود لوگین استفاده میکنم
فقط مشکلی هست من به پیج دلخواه نمیرم و همش به Default.aspx میروم
<asp:Login ID="Login1" runat="server" BorderStyle="Solid"
FailureText="ورود ناموفق بود" PasswordLabelText="پسورد :"
RememberMeText="مرا به یاد داشته باش" TitleText=""
UserNameLabelText="نام کاربری :" onloggedin="Login1_LoggedIn"
LoginButtonText="ورود به سایت">
</asp:Login>
وب کنفیگ
<authentication mode="Forms">
<forms loginUrl="LoginToSite.aspx" />
</authentication>
if (this.Page.User.IsInRole("Administrator"))
{
Response.Redirect("Administrator/Default.aspx");
}

if (this.Page.User.IsInRole("PowerUser"))
{
Response.Redirect("PowerUser/Default.aspx");
}
}

fakhravari
دوشنبه 09 آبان 1390, 13:13 عصر
دوستان کسی یاری میکنه

fakhravari
دوشنبه 09 آبان 1390, 13:24 عصر
if (User.IsInRole("Administrator"))
{
Response.Redirect("Administrator/Default.aspx");
}

else if( User.IsInRole("PowerUser"))
{
Response.Redirect("PowerUser/Default.aspx");
}
با این جواب میده :گیج: