Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to download some code to look how it's done you can take nerdinner's : <a href="http://nerddinner.codeplex.com/" rel="nofollow">http://nerddinner.codeplex.com/</a></p> <p>First you need to have to be sure youre web.config configuration is ok</p> <pre><code>&lt;connectionStrings&gt; &lt;add name="XXXXXMembership" connectionString="data source=.\SQLEXPRESS;Initial Catalog=corpiq_membership;User Id=corpiq; Password=c0rp1q; Persist Security Info=true;" providerName="System.Data.SqlClient" /&gt; &lt;add name="CorpiqDb" connectionString="data source=.\SQLEXPRESS;Initial Catalog=corpiq;User Id=corpiq; Password=c0rp1q; Persist Security Info=true;" providerName="System.Data.SqlClient" /&gt; &lt;/connectionStrings&gt; &lt;membership&gt; &lt;providers&gt; &lt;clear/&gt; &lt;add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="XXXXXMembership" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="3" minRequiredPasswordLength="8" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="30" passwordStrengthRegularExpression="^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$" passwordFormat="Hashed" applicationName="/" /&gt; &lt;/providers&gt; &lt;/membership&gt; </code></pre> <p>If everything is ok you should be able to launch the ASP.Net Configuration tool, witch is th hammer (in red) and planet on top of the solution explorer when you are on the MVC website. With that tools you can add user and roles.</p> <p>After you should be able to simply add this line in your controller :</p> <pre><code>[Authorize(Roles = "Member, Delegate")] </code></pre> <p>And I would suggest writing a wrapper that call Membership method so you can have youre own logic, here's mine :</p> <pre><code>public class AuthenticationService : IAuthenticationService { public bool IsValidLogin(string email, string password) { //Unlock user if it makes more than 30 minutes CheckLocked(email); return Membership.ValidateUser(email, password); } public void SignIn(string email, bool createPersistentCookie) { if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); FormsAuthentication.SetAuthCookie(email, createPersistentCookie); } public void SignOut() { FormsAuthentication.SignOut(); } public string GetLoggedInUserName() { return Membership.GetUser() != null ? Membership.GetUser().UserName : string.Empty; } public MembershipCreateStatus RegisterUser(string email, string password, string role) { MembershipCreateStatus status; Membership.CreateUser(email, password, email, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, out status); if (status == MembershipCreateStatus.Success) { Roles.AddUserToRole(email, role); } return status; } public MembershipUserCollection GetAllUsers() { return Membership.GetAllUsers(); } public string GeneratePassword() { var alphaCaps = "QWERTYUIOPASDFGHJKLZXCVBNM"; var alphaLow = "qwertyuiopasdfghjklzxcvbnm"; var numerics = "1234567890"; var special = "@#$"; var allChars = alphaCaps + alphaLow + numerics + special; var r = new Random(); var generatedPassword = ""; for (int i = 0; i &lt; MinPasswordLength - 1; i++) { double rand = r.NextDouble(); if (i == 0) { //First character is an upper case alphabet generatedPassword += alphaCaps.ToCharArray()[(int)Math.Floor(rand * alphaCaps.Length)]; //Next one is numeric rand = r.NextDouble(); generatedPassword += numerics.ToCharArray()[(int) Math.Floor(rand*numerics.Length)]; } else { generatedPassword += allChars.ToCharArray()[(int)Math.Floor(rand * allChars.Length)]; } } return generatedPassword; } public int MinPasswordLength { get { return Membership.Provider.MinRequiredPasswordLength; } } public string AdminRole { get { return "admin"; } } public string MemberRole { get { return "member"; } } public string DelegateRole { get { return "delegate"; } } public bool Delete(string email) { return Membership.DeleteUser(email); } public bool IsAdmin() { return Roles.IsUserInRole(AdminRole); } public bool IsMember() { return Roles.IsUserInRole(MemberRole); } public bool IsDelegate() { return Roles.IsUserInRole(DelegateRole); } public bool ChangePassword(string email, string oldPassword, string newPassword) { if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword"); if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword"); // The underlying ChangePassword() will throw an exception rather // than return false in certain failure scenarios. try { var currentUser = Membership.Provider.GetUser(email, true); return currentUser.ChangePassword(oldPassword, newPassword); } catch (ArgumentException) { return false; } catch (MembershipPasswordException) { return false; } } public string ResetPassword(string email) { if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); var currentUser = Membership.Provider.GetUser(email, false); return currentUser.ResetPassword(); } public bool CheckLocked(string email) { if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); var currentUser = Membership.Provider.GetUser(email, false); if (currentUser == null) return false; if (!currentUser.IsLockedOut) return false; if (currentUser.LastLockoutDate.AddMinutes(30) &lt; DateTime.Now) { currentUser.UnlockUser(); return false; } return true; } public bool Unlock(string email) { if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); var currentUser = Membership.Provider.GetUser(email, false); if (currentUser == null) return false; currentUser.UnlockUser(); return true; } public void CheckRoles() { if (!Roles.RoleExists(MemberRole)) Roles.CreateRole(MemberRole); if (!Roles.RoleExists(AdminRole)) Roles.CreateRole(AdminRole); if (!Roles.RoleExists(DelegateRole)) Roles.CreateRole(DelegateRole); } } </code></pre> <p>I'm not quite sure witch part you don't understand but let us know in details what are your problems and maybe we could help more! I think you need to get youre code building first.</p> <p>And here's a good start for EF (so you can write youre custom profile/user in youre own database) : <a href="http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx" rel="nofollow">http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload