Membership 使用方法

摘要: Membership 強 制 使 用 者 登 出

 

##################   強制登出   ########################

using System.Web.Security;

    FormsAuthentication.SignOut();
    FormsAuthentication.RedirectToLoginPage();

 

 

################  登入控制項 #######################

          <asp:Login ID="LoginUser" runat="server" Height="181px" Width="500px" UserNameLabelText="使用者帳號:"
                        BorderPadding="0" DestinationPageUrl="~/Default.aspx">
                        <LayoutTemplate>
                            <div id="login_txt">
                                <ul>
                                    <li>
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">帳號:</asp:Label></li>
                                    <li>
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密碼:</asp:Label></li>
                                </ul>
                            </div>
                            <div id="login_txt_box">
                                <ul>
                                    <li>
                                        <asp:TextBox ID="UserName" runat="server" Font-Bold="true"      CssClass="login_id_txtbox" ></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ForeColor="Red" ControlToValidate="UserName"
                                            ErrorMessage="必須提供使用者名稱。" ToolTip="必須提供使用者名稱。" ValidationGroup="LoginUser" Display="Dynamic">請輸入帳號</asp:RequiredFieldValidator>
                                        <li>
                                            <asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="login_pw_txtbox"></asp:TextBox> <br />
                                            <asp:RequiredFieldValidator ID="PasswordRequired" runat="server"  ForeColor="Red"   ControlToValidate="Password"
                                                ErrorMessage="必須提供密碼。" ToolTip="必須提供密碼。" ValidationGroup="LoginUser" Display="Dynamic">請輸入密碼</asp:RequiredFieldValidator><br /><br />
                                      <div  style="padding-top:5px;"><font color="red"><b>
                                            <asp:Literal ID="FailureText" runat="server"   EnableViewState="False"></asp:Literal></b></font>  </div>
                                        </li>
                                        <br/>
                                           <div id="capslock" style="display: none; font-size:12px; color:Red; font-weight:bold; line-height:15px; margin:10px 10px 10px 10px; position:absolute; top:130px;">
                                您的鍵盤現在設定為大寫模式!可能造成您的密碼錯誤,您可按鍵盤上的Caps Lock鍵切回小寫!
                            </div>
                                </ul>
                            </div>
                            <div class="login_btn">
                                <asp:ImageButton ID="LoginButton" runat="server" CommandName="Login" Text="登入" Width="40px" Height="40px" ImageUrl="~/App_Themes/Know/images/btn_login_m.gif" />
                            </div>
                            <div class="sign_txt">
                                <a href="~/login/forget.aspx" runat="server"  enableviewstate="false" title="忘記密碼">忘記密碼</a>|<a href="~/login/Service.aspx"
                                   runat="server" enableviewstate="false"  title="我要新註冊">我要新註冊</a></div>
                            </div>
                   
                        </LayoutTemplate>
                    </asp:Login>

_______________________________________________________________________________________

 

#####################   新增使用者   ############################

   Membership.CreateUser(txtAccount.Text.Trim(), txtPasswd.Text.Trim(), txtEmail.Text.ToString().Trim(), txtAccount.Text.Trim(), txtAccount.Text.Trim(), true, out status);
            if (status.ToString() == "Success")
            {
                ProfileCommon Pf;
                Pf = Profile.GetProfile(txtAccount.Text);
                ucFileUpload.UserName = txtAccount.Text;
                Pf.個人資訊.圖片位址 = ucFileUpload.SaveAs(out yErrMsg);
                if (yErrMsg != "")
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "jsError", "alert('" + yErrMsg + "');", true);

                Pf.個人資訊.暱稱 = txtName.Text;

                Pf.個人資訊.性別 = radSex.SelectedValue;
                Pf.個人資訊.職業 = dropJob.SelectedValue.ToString();
                Pf.個人資訊.生日 = ucDate.SelectedDate.ToString();

                Pf.個人資訊.縣市名稱 = ucArea._CityName;
                Pf.個人資訊.鄉鎮名稱 = ucArea._DistrcitName;

                Pf.個人資訊.學歷 = dropBestSchoolRecord.SelectedValue;

                Pf.個人資訊.建立日期 = System.DateTime.Now.ToString();
                Pf.個人資訊.IP = Request.UserHostAddress;
                Pf.個人資訊.學歷 = dropBestSchoolRecord.SelectedValue;


                //確認使用者是否在群組中
                if (!Roles.IsUserInRole(txtAccount.Text, "一般使用者 "))
                {   //加入使用者至群組
                    Roles.AddUserToRole(txtAccount.Text, "一般使用者 ");

                    //儲存個人資訊
                    Pf.Save();
                }

                // SendMail();//寄信

                if (this.Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "jsError") == false)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "jsError", "alert('已成功完成註冊!');location.href='Login.aspx';", true);
                }
            }

_________________________________________________________________________________

 

#####################  Membership Profile 設定 ##################################

<profile defaultProvider="AspNetSqlProfileProvider">
   <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" connectionStringName="CienveAccountServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
   </providers>
   <properties>
    <group name="個人資訊">
     <add name="暱稱" type="System.String"/>
     <add name="職業" type="System.String"/>
     <add name="性別" type="System.String"/>

______________________________________________________________________________________

 

#####################      讀取個人Membership角色資料          ##################################

       MembershipUser CurrentUser;
        CurrentUser = Membership.GetUser();

        labAccount.Text = CurrentUser.UserName.ToString();//帳號
        txtEmail.Text= CurrentUser.Email.ToString();//Email

        if (Roles.IsUserInRole(CurrentUser.UserName, "一般使用者"))
            labRole.Text = "一般使用者";
        else if (Roles.IsUserInRole(CurrentUser.UserName, "環保署"))
            labRole.Text = "環保署";
        else if (Roles.IsUserInRole(CurrentUser.UserName, "系統管理者"))
            labRole.Text = "系統管理者";
        else
           labRole.Text = "尚未設定角色權限";


        ProfileCommon Pf;
        Pf = Profile.GetProfile(CurrentUser.UserName);


        txtName.Text = Pf.個人資訊.暱稱;//暱稱

______________________________________________________________

 

####################  俢改個人資料 #############################

  MembershipUser CurrentUser;
        CurrentUser = Membership.GetUser();
        ProfileCommon Pf = Profile.GetProfile(CurrentUser.UserName);

        Pf.個人資訊.縣市名稱 = ucArea._CityName;
        Pf.個人資訊.鄉鎮名稱 = ucArea._DistrcitName;
        Pf.個人資訊.生日 = ucData.SelectedDate.ToString();
        Pf.個人資訊.性別 = radSex.SelectedValue;
        Pf.個人資訊.暱稱 = txtName.Text;
        Pf.個人資訊.職業 = dropJob.SelectedValue;
        Pf.個人資訊.學歷 = dropBestSchoolRecord.SelectedValue;

        #region <  ->儲存圖片  >

        string FileURL = "";
        ucFileUpload.UserName = labAccount.Text;
        FileURL=ucFileUpload.SaveAs(out yErrMsg);

        if (FileURL != "") Pf.個人資訊.圖片位址 = FileURL;
        if (yErrMsg != "")
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "jsError", "alert('" + yErrMsg + "');", true);
        #endregion

        CurrentUser.Email = txtEmail.Text.Trim();
        Membership.UpdateUser(CurrentUser);

        Pf.Save(); //儲存個人資訊