نمایش نتایج 1 تا 40 از 67

نام تاپیک: آموزشی + ساخت لاگین

Threaded View

پست قبلی پست قبلی   پست بعدی پست بعدی
  1. #7
    کاربر دائمی آواتار z_bluestar
    تاریخ عضویت
    مرداد 1385
    محل زندگی
    تهران
    سن
    39
    پست
    359
    یک کلاس در پروژه تون ایجاد کنید
    و اون رو Inherits کنید از کلاس RoleProvider
    public class MySiteRoleProvider: System.Web.Security.RoleProvider

    کلاس RoleProvider یک کلاس Abstract می باشد و شما باید یک سری از متدهاش رو پیاده سازی کنید برای این کار بر روی خط تیره زیر نام کلاس RoleProvider کلیک کرده و گزینه Implement abstract class System.Web.Security.RoleProvider
    را انتخاب نمایید .



    بعد از Implement کردن کلاس یک سری متد به آن اضافه می شه

    public class MySiteRoleProvider: System.Web.Security.RoleProvider
    {
    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
    throw new NotImplementedException();
    }

    public override string ApplicationName
    {
    get
    {
    throw new NotImplementedException();
    }
    set
    {
    throw new NotImplementedException();
    }
    }

    public override void CreateRole(string roleName)
    {
    throw new NotImplementedException();
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
    throw new NotImplementedException();
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
    throw new NotImplementedException();
    }

    public override string[] GetAllRoles()
    {
    throw new NotImplementedException();
    }

    public override string[] GetRolesForUser(string username)
    {
    throw new NotImplementedException();
    }

    public override string[] GetUsersInRole(string roleName)
    {
    throw new NotImplementedException();
    }

    public override bool IsUserInRole(string username, string roleName)
    {
    throw new NotImplementedException();
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
    throw new NotImplementedException();
    }

    public override bool RoleExists(string roleName)
    {
    throw new NotImplementedException();
    }
    }


    حالا ما می تونیم هر کدام از این متدها رو بر حسب نیازمون تغییر بدیم مثلا برای متد GetRolesForUser

    public override string[] GetRolesForUser(string username)
    {
    string[] UserRolename=new string[1];
    UserRolename[0] = DataLayer.Users.RoleName(username); // خوندن نام نقش کاربر از دیتابیس
    return UserRolename;
    }


    برای این کار یه تغییر هم باید در web.config بدهید
    <system.web>
    <roleManager defaultProvider="SiteRoleProvider" enabled="true">
    <providers>
    <clear/>
    <add name="SiteRoleProvider" type="abcconstruct.Classes.MySiteRoleProvider"/>
    </providers>
    </roleManager>


    abcconstruct.Classes نام name space کلاسم هست

    حالا برای استفاده کردن از این می تونیم دو روش استفاده کنیم :
    1- در web.config فولدری که می خواهین مثلا فقط برای کاربران admin قابل مشاهده باشد این را بنویسید :

    <system.web>
    <authorization>
    <allow roles="Administrator"/>
    <deny users="*"/>
    </authorization>
    </system.web>
    </configuration>


    یا در صفحه ای که می خواهید Role کاربر را چک کنید این کد را بویسید .
    String[] Role = RoleProvider.GetRolesForUser(txtLUsername.Text);
    if (Role[0] == "Company")
    Response.Redirect("~/Customer/");
    else
    Response.Redirect("~/User/");


    یا برای چک کردن این که آیا این کاربر دارای نقش مورد نظر هست یا نه ؟؟

     bool IsinRole = HttpContext.Current.User.IsInRole("Administrator")  ;


    ارسال ایمیل :

    using System;
    using System.Web.Mail;
    namespace SMTPAuthentication
    {
    public class SMTPAuthenticationExample
    {
    public static void SendMail()
    {
    string smtpServer = "mail.yourdomain.com";
    string userName = "you@yourdomain.com";
    string password = "YourMailboxPassword";
    int cdoBasic = 1;
    int cdoSendUsingPort = 2;
    MailMessage msg = new MailMessage();
    if (userName.Length > 0)
    {
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25) ;
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort) ;
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
    }
    msg.To = "someone@domain.com";
    msg.From = "you@yourdomain.com";
    msg.Subject = "Subject";
    msg.Body = "Message";
    SmtpMail.SmtpServer = smtpServer;
    SmtpMail.Send(msg);
    }
    }
    }
    آخرین ویرایش به وسیله raziee : یک شنبه 18 دی 1390 در 00:52 صبح دلیل: ادغام کردن پست ها

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •