开发者

Unable to get the Role using Context.User.IsRole

I writ开发者_开发百科e the following code to access the page but this is not working for me

if (User.Identity.IsAuthenticated)
    {
        if (Context.User.IsInRole("DistrictAdmin"))
        {
            if (!IsPostBack)
            {
            }
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
    else
    {
        Response.Redirect("Default.aspx");
    } 

My Login check code

  string RoleTypeID;
    objLogin.UserName = txtUsername.Text;
    objLogin.Password = txtPassword.Text;

    if (objLogin.getRoles(out RoleTypeID))
    {
        Session["RoleID"] = RoleTypeID;
        FormsAuthenticationTicket oAuthTicket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(20), false, RoleTypeID.ToString(), FormsAuthentication.FormsCookiePath);
        string encryptoAuthTicket = FormsAuthentication.Encrypt(oAuthTicket);


        HttpCookie oCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptoAuthTicket); // Name of auth cookie

        if (oAuthTicket.IsPersistent) oCookie.Expires = oAuthTicket.Expiration;

        HttpContext.Current.Response.Cookies.Add(oCookie);
        if (RoleTypeID == "DistrictAdmin" || RoleTypeID == "CampusAdministrator" || RoleTypeID == "LPACMember")
        {
            Response.Redirect("LEPstudentrecords.aspx");
        }
    }
    else
    {
        lblInvalid.Visible = true;
    }

and in my web.config i set as follows

     <roleManager enabled="false" />
    <authentication mode="Forms">
  <forms loginUrl="Default.aspx"
timeout="20" />
</authentication>

    <sessionState mode="InProc" timeout="180"></sessionState>

But i am unable to get the role even if he is authenticated can any one tell what to do

Here is the image after authenticated every thing is getting right but i am unable to access that page

Unable to get the Role using Context.User.IsRole


You are adding the role string as 'UserData' to the FormsAuthenticationTicket. There is no magic that will infer this value to a role.

I suggest you consult the documentation again.

http://msdn2.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.formsauthenticationticket

See http://www.codeproject.com/KB/web-security/formsroleauth.aspx for an example without RoleManager.


That is because you have not configured the RoleManager. In your web.Config it is disabled.

<roleManager enabled="false" />

You need to use an exisiting RoleProvider or write your own.

Sample configuration

<roleManager enabled="true" defaultProvider="SqlRoleManager">
  <providers>
    <add name="SqlRoleManager" 
         type="System.Web.Security.SqlRoleProvider"
         connectionStringName="SqlRoleManagerConnection"
         applicationName="MyApplication" />
  </providers>
</roleManager>

Here are some links about role providers

RoleProvider

Implementing Custom Role Provider

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜