में डिफ़ॉल्ट प्रदाता के रूप में अपनी सदस्यता प्रदाता रजिस्टर तो यह मेरे थे अपने ईमेल परिवर्तन जोड़ सकते हैं, मैं शायद इसके बारे में निम्न तरीके से जाऊंगा:
सबसे पहले, मान लें कि आप एंटिटी फ्रेमवर्क या कुछ दा के साथ सरलमेम्बरशिप का उपयोग कर रहे हैं टैबस कनेक्शन (एडीओ, LINQ से SQL, आदि) आपके पास दो घटक होंगे: WebSecurity.*
विधि कॉल, और प्रोफ़ाइल परिवर्तन करने के लिए डेटाबेस कनेक्शन। व्यक्तिगत रूप से, मैं यह सुनिश्चित करने के लिए डेटाबेस में CONSTRAINT
डेटाबेस जोड़ दूंगा, लेकिन आप इस तर्क को संभालने वाली सदस्यता सेवा भी कार्यान्वित कर सकते हैं।
सबसे पहले, एक अंतरफलक है कि आपके नियंत्रक में संदर्भित किया जा सकता (जैसे कुछ के बाद) के लिए समूह इन:
public interface IMembershipService
{
Int32 CurrentUserId { get; }
String CurrentUserName { get; }
Boolean IsAuthenticated { get; }
Boolean CreateUserAndAccount(String username, String password, String emailaddress = null);
Boolean CreateUserAndAccount(String username, string password, out String confirmationToken, String emailaddress = null);
Boolean Login(String username, String password, Boolean persistCookie = false);
void Logout();
}
तो फिर आप अपने डेटाबेस कनेक्शन SimpleMembership का एक संकर और के रूप में सेवा को लागू कर सकते हैं। इसे सामान्य रखने के लिए, मैं IRepository<T>
पैटर्न का उपयोग करता हूं, लेकिन यह प्रत्यक्ष DbContext
, ObjectContext
आदि हो सकता है। मैं इसे संक्षिप्त भी रख रहा हूं, इसलिए लापता चेकसम और छोटे कार्यान्वयन को क्षमा करें।
public class MembershipService : IMembershipService
{
protected readonly SimpleMembershipProvider membershiProvider;
protected readonly SimpleRoleProvider roleProvider;
protected readonly IRepository<UserProfile> profileRepository;
public MembershipService(IRepository<UserProfile> profileRepository)
{
this.membershipProvider = Membership.Provider as SimpleMembershipProvider;
this.roleProvider = Role.Provider as SimpleRoleProvider;
this.profileRepository = userRepository;
}
#region IMembershipService Implementation
public Int32 CurrentUserId
{
get { return WebSecurity.CurrentUserId; }
}
public String CurrentUserName
{
get { return WebSecurity.CurrentUserName; }
}
public Boolean IsAuthenticated
{
get { return WebSecurity.IsAuthenticated; }
}
public Boolean CreateUserAndAccount(String username, String password, String emailaddress = null)
{
// validate the email address is unique
if (!this.profileRepository.Any(x => x.EmailAddress == emailaddress))
{
WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
// handle the error how you see fit
// (maybe even exception?)
return false;
}
}
public Boolean CreateUserAndAccount(String username, String password, out String confirmationToken, String emailaddress = null, out)
{
// validate the email address is unique
if (this.profileRepository.First(x => x.EmailAddress == emailaddress) == null)
{
confirmationToken = WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
// handle the error how you see fit
// (maybe even exception?)
confirmationToken = String.Empty;
return false;
}
}
public Boolean Login(String username, String password, Boolean persistCookie = false)
{
return WebSecurity.Login(username, password, persistCookie);
}
public void Logout()
{
WebSecurity.Logout();
}
#endregion
}
अब आप इस इंटरफ़ेस को अपने नियंत्रक में संदर्भित कर सकते हैं और तर्क एक ही स्थान पर कर सकते हैं। यदि आप एक डि कंटेनर का उपयोग कर रहे हैं, जाहिर है यह रजिस्टर, लेकिन यहाँ एक उदाहरण दिया गया है:
public class AccountController: Controller
{
private readonly IMembershipService membershipService;
public AccountController(IMembershipService membershipService)
{
this.membershipService = membershipService;
}
/* ... */
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Register(LoginViewModel model, String returnUrl)
{
if (ModelState.IsValid)
{
if (this.membershipService.CreateUserandAccount(model.Username, model.Password, model.EmailAddress))
{
this.membershipService.Login(model.Username, model.Password);
if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToRoute("Default");
}
else
{
ModelState.AddModelError("", "Unable to register.");
}
}
return View(model);
}
/* ... */
}
आप EntityFramework का उपयोग कर रहे हैं, तो आप भी IValidatableObject
उपयोग कर सकते हैं। नकल का विरोध करने के लिए, यहाँ एक और एसओ प्रश्न/उत्तर है कि एक अद्वितीय प्रविष्टि के लिए जाँच करता है:
Entity Framework IValidatableObject
अगर यह एक नया डेटाबेस है, एक और विकल्प एक उपयोगकर्ता नाम के रूप में ईमेल का उपयोग करना होगा। उपयोगकर्ता नाम डिफ़ॉल्ट रूप से अद्वितीय है। – Igarioshka