PDA

View Full Version : آموزش: Membership.CreateUserچیست؟



m4prog
پنج شنبه 15 اردیبهشت 1390, 12:58 عصر
با سلام
لطفا توضیحاتی در رابطه با این دستور بدید که چه کار هایی انجام می دهد

clover
یک شنبه 18 اردیبهشت 1390, 09:56 صبح
دوست من، این تابع برای ایجاد کاربر جدید در سیستم Membership مایکروسافت استفاده میشه. Overload های مختلفی داره که ساده ترینش به این شکل هست:

public static MembershipUser CreateUser(
string username,
string password
)
کافیه یک نام کاربری و یک پسورد به این تابع پاس بدی تا در صورت امکان کاربر جدید را به دیتابیس اضافه کنه و اونو به صورت خروجی از نوع MembershipUser برگردونه، در صورتی که به هر علتی خطایی اتفاق بیفته یک Exeption از نوع MembershipCreateUserException تولید میشه. اما اگر بخوایی به جای مدیریت خطا از وضعیت عملیات و نتیجه اطلاع پیدا کنی می تونی از Overload های سوم یا چهارم این تابع استفاده کنی که یک پارامتر خروجی به نام MembershipCreateStatus میگیره و وضعیت ایجاد کاربر را از این طریق بر می گردونه:

public static MembershipUser CreateUser(
string username,
string password,
string email,
string passwordQuestion,
string passwordAnswer,
bool isApproved,
Object providerUserKey,
out MembershipCreateStatus status
)

این هم یک مثال:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void CreateUser_OnClick(object sender, EventArgs args)
{
// Create new user and retrieve create status result.

MembershipCreateStatus status;
string passwordQuestion = "";
string passwordAnswer = "";

if (Membership.RequiresQuestionAndAnswer)
{
passwordQuestion = PasswordQuestionTextbox.Text;
passwordAnswer = PasswordAnswerTextbox.Text;
}

try
{
MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text,
EmailTextbox.Text, passwordQuestion,
passwordAnswer, true, out status);
if (newUser == null)
{
Msg.Text = GetErrorMessage(status);
}
else
{
Response.Redirect("login.aspx");
}
}
catch
{
Msg.Text = "An exception occurred creating the user.";
}
}

public string GetErrorMessage(MembershipCreateStatus status)
{
switch (status)
{
case MembershipCreateStatus.DuplicateUserName:
return "Username already exists. Please enter a different user name.";

case MembershipCreateStatus.DuplicateEmail:
return "A username for that e-mail address already exists. Please enter a different e-mail address.";

case MembershipCreateStatus.InvalidPassword:
return "The password provided is invalid. Please enter a valid password value.";

case MembershipCreateStatus.InvalidEmail:
return "The e-mail address provided is invalid. Please check the value and try again.";

case MembershipCreateStatus.InvalidAnswer:
return "The password retrieval answer provided is invalid. Please check the value and try again.";

case MembershipCreateStatus.InvalidQuestion:
return "The password retrieval question provided is invalid. Please check the value and try again.";

case MembershipCreateStatus.InvalidUserName:
return "The user name provided is invalid. Please check the value and try again.";

case MembershipCreateStatus.ProviderError:
return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

case MembershipCreateStatus.UserRejected:
return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

default:
return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
}
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Create User</title>
</head>
<body>

<form id="form1" runat="server">
<h3>Create New User</h3>

<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

<table cellpadding="3" border="0">
<tr>
<td>Username:</td>
<td><asp:Textbox id="UsernameTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UserNameTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
ControlToValidate="PasswordTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" />
<asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ControlToCompare="PasswordTextBox"
ErrorMessage="Confirm password must match password." />
</td>
</tr>
<tr>
<td>Email Address:</td>
<td><asp:Textbox id="EmailTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
ControlToValidate="EmailTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>


<% if (Membership.RequiresQuestionAndAnswer) { %>

<tr>
<td>Password Question:</td>
<td><asp:Textbox id="PasswordQuestionTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordQuestionRequiredValidator" runat="server"
ControlToValidate="PasswordQuestionTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password Answer:</td>
<td><asp:Textbox id="PasswordAnswerTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordAnswerRequiredValidator" runat="server"
ControlToValidate="PasswordAnswerTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>

<% } %>


<tr>
<td></td>
<td><asp:Button id="CreateUserButton" Text="Create User" OnClick="CreateUser_OnClick" runat="server" /></td>
</tr>
</table>
</form>

</body>
</html>

لینک منبع (http://msdn.microsoft.com/en-us/library/system.web.security.membership.createuser.aspx)

موفق باشی.

farshid1874
سه شنبه 26 اسفند 1393, 11:11 صبح
دوست عزیز تابع MembershipUser CreateUser که نوشتید رو کجا میشه بهش دسترسی داشت؟