Reputation: 51
Hello and thanks in advance
I'm lost and i don't know whats wrong with my code, and i've been trying to follow a tutorial found at http://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx and making changes to match my own DB.
firstly here is the relevant part of my DB script (ssms)
CREATE TABLE BFS.dbo.BFSSUSER(
ID int IDENTITY(1,1),
RoleID int,
Name nvarchar(50)NOT NULL,
SiteName nvarchar(50)NOT NULL,
UserPassword nvarchar(20),
CONSTRAINT BFSSUSER_pk PRIMARY KEY (ID))
GO
USE BFS
GO
CREATE TABLE BFS.dbo.ROLE(
ID int IDENTITY(1,1),
Name nvarchar(50)NOT NULL
CONSTRAINT ROLE_pk PRIMARY KEY (ID),
CONSTRAINT ROLE_fk FOREIGN KEY (ID) REFERENCES BFSUSER(ID))
GO
okay now Im trying to make my providers and using a repository class
I will include my entire code on it but ill identify from where my errors start and i don't know what to do
(all the if roleExists statements in mine are red and driving me nuts (34 errors!!!), I have tried several combinations to get them to go away and (well i suck basically) any and all help would be greatly appreciated (bare in mind i m brand spanking new to asp.net and have no idea what anything is or means!!)
here's my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace BFS.Models
{
public class BFSRepository
{
private BFSEntities entities = new BFSEntities();
private const string MissingRole = "Role does not exist";
private const string MissingUser = "User does not exist";
private const string TooManyUser = "User already exists";
private const string TooManyRole = "Role already exists";
private const string AssignedRole = "Cannot delete a role with assigned users";
#region Properties
public int NumberOfUsers
{
get
{
return this.entities.BFSUSERs.Count();
}
}
public int NumberOfRoles
{
get
{
return this.entities.ROLEs.Count();
}
}
#endregion
#region Constructors
public BFSRepository()
{
this.entities = new BFSEntities();
}
#endregion
#region Query Methods
public IQueryable<BFSUSER> GetAllUsers()
{
return from user in entities.BFSUSERs
orderby user.ID
select user;
}
public BFSUSER GetUser(int id)
{
return entities.BFSUSERs.SingleOrDefault(user => user.ID == id);
}
public BFSUSER GetUser(string userName)
{
return entities.BFSUSERs.SingleOrDefault(user => user.SiteName == userName);
}
FROM HERE ON DOWN IS WHERE ALL MY ERRORS START
"entities does not contain a definition for 'Roles' etc etc missing assem ref
public IQueryable<BFSUSER> GetUsersForRole(string Name)
{
return GetUsersForRole(ROLE(Name));
}
public IQueryable<BFSUSER> GetUsersForRole(int id)
{
return GetUsersForRole(GetRole(id));
}
public IQueryable<ROLE> GetUsersForRole(Roles role)
{
if (!ExistsROLE(role))
throw new ArgumentException(MissingRole);
return from user in entities.BFSUSERs
where user.RoleID == role.ID
orderby user.SiteName
select user;
}
public IQueryable<ROLE> GetAllRoles()
{
return from role in entities.ROLEs
orderby role.Name
select role;
}
public ROLE GetRole(int id)
{
return entities.ROLEs.SingleOrDefault(role => role.ID == id);
}
public ROLE GetRole(string name)
{
return entities.ROLEs.SingleOrDefault(role => role.Name == name);
}
public ROLE GetRoleForUser(string SiteName)
{
return GetRoleForUser(GetUser(SiteName));
}
public ROLE GetRoleForUser(int id)
{
return GetRoleForUser(GetUser(id));
}
public ROLE GetRoleForUser(User user)
{
if (!UserExists(user))
throw new ArgumentException(MissingUser);
return user.Role;
}
#endregion
#region Insert/Delete
private void AddUser(BFSUSER user)
{
if (BFSUSERExists(user))
throw new ArgumentException(TooManyUser);
entities.BFSUSERs.Add(user);
}
public void CreateUser(string name, string SiteName, string UserPassword, string roleName)
{
ROLE role = GetRole(roleName);
if (string.IsNullOrEmpty(SiteName.Trim()))
throw new ArgumentException("The user name provided is invalid. Please check the value and try again.");
if (string.IsNullOrEmpty(name.Trim()))
throw new ArgumentException("The name provided is invalid. Please check the value and try again.");
if (string.IsNullOrEmpty(UserPassword.Trim()))
throw new ArgumentException("The password provided is invalid. Please enter a valid password value.");
if (!roleExists(role))
throw new ArgumentException("The role selected for this user does not exist! Contact an administrator!");
if (this.entities.BFSUSERs.Any(user => user.SiteName == SiteName))
throw new ArgumentException("Username already exists. Please enter a different user name.");
newUser = new User()
{
UserName = SiteName,
Name = name,
Password = FormsAuthentication.HashPasswordForStoringInConfigFile(UserPassword.Trim(), "md5"),
RoleID = role.ID
};
try
{
AddUser(newUser);
}
catch (ArgumentException ae)
{
throw ae;
}
catch (Exception e)
{
throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
"If the problem persists, please contact your system administrator.");
}
// Immediately persist the user data
Save();
}
public void DeleteUser( user)
{
if (!UserExists(user))
throw new ArgumentException(MissingUser);
entities.BFSUSERs.DeleteObject(user);
}
public void DeleteUser(string userName)
{
DeleteUser(GetUser(userName));
}
public void AddRole(Roles role)
{
if (RolesExists(role))
throw new ArgumentException(TooManyRole);
entities.Roles.AddObject(role);
}
public void AddRole(string roleName)
{
Role role = new Role()
{
Name = roleName
};
AddRole(role);
}
public void DeleteRole(Role role)
{
if (!RoleExists(role))
throw new ArgumentException(MissingRole);
if (GetUsersForRole(role).Count() > 0)
throw new ArgumentException(AssignedRole);
entities.Roles.DeleteObject(role);
}
public void DeleteRole(string roleName)
{
DeleteRole(GetRole(roleName));
}
#endregion
#region Persistence
public void Save()
{
entities.SaveChanges();
}
#endregion
#region Helper Methods
public bool UserExists(User user)
{
if (user == null)
return false;
return (entities.Users.SingleOrDefault(u => u.ID == user.ID || u.UserName == user.UserName) != null);
}
public bool RoleExists(Roles role)
{
if (role == null)
return false;
return (entities.BFSUSERs.SingleOrDefault(r => r.ID == role.ID || r.Name == role.Name) != null);
}
#endregion
}
}
ANY AND ALL HELP would be greatly appreciated
especially explaining or showing to me when it needs to be the Table Name, variable Name etc. I'm confused as my Table name is Role and what I'm doing is role
Upvotes: 0
Views: 305
Reputation: 1186
Creating an custom membership provider in a few steps:
throw new NotImplementedException();
bodypublic override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
public override MembershipUser GetUser(string username, bool userIsOnline)
public override bool ValidateUser(string username, string password)
Configure the web.config for using your custom membership provider:
<membership defaultProvider="CurrentProvider" userIsOnlineTimeWindow="10">
<providers>
<add name="CurrentProvider" Type="YOUR.WEBAPP.PROVIDERS.CustomMembershipProvider" />
</providers>
</membership>
for MVC4 must add or set the following AppSettings:
<add key="enableSimpleMembership" value="false" />
<add key="autoFormsAuthentication" value="false" />
For a custom role provider it's nearly the same procedure
RoleProvider
(MVC3 and 4) for inheritance public override bool IsUserInRole(string username, string roleName)
public override string[] GetRolesForUser(string username)
<roleManager defaultProvider="CurrentProvider" enabled="true">
<providers>
<add name="CurrentProvider" type="YOUR.WEBAPP.PROVIDERS.CustomRoleProvider" />
</providers>
</roleManager>
If when you use membership functions and an NotImplementedException is throw you know you have to override the method where the exception is thrown.
Upvotes: 1