Files
MileageTraker/Web/Membership/WebSecurity.cs
T

176 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using MileageTraker.Web.Context;
namespace MileageTraker.Web.Membership
{
public static class WebSecurity
{
public static HttpContextBase Context
{
get { return new HttpContextWrapper(HttpContext.Current); }
}
public static HttpRequestBase Request
{
get { return Context.Request; }
}
public static HttpResponseBase Response
{
get { return Context.Response; }
}
public static IPrincipal User
{
get { return Context.User; }
}
public static bool IsAuthenticated
{
get { return User.Identity.IsAuthenticated; }
}
public static MembershipCreateStatus Register(string username, string password, string email, bool isApproved,
string firstName, string lastName)
{
MembershipCreateStatus createStatus;
System.Web.Security.Membership.CreateUser(username, password, email, null, null, isApproved, Guid.NewGuid(),
out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
using (var context = new MileageTrakerContext())
{
var user = context.Users.First(usr => usr.Username == username);
user.FirstName = firstName;
user.LastName = lastName;
context.SaveChanges();
}
if (isApproved)
{
FormsAuthentication.SetAuthCookie(username, false);
}
}
return createStatus;
}
public static Boolean Login(string username, string password, bool persistCookie = false)
{
var success = System.Web.Security.Membership.ValidateUser(username, password);
if (success)
{
FormsAuthentication.SetAuthCookie(username, persistCookie);
}
return success;
}
public static void Logout()
{
FormsAuthentication.SignOut();
}
public static MembershipUser GetUser(string username)
{
return System.Web.Security.Membership.GetUser(username);
}
public static bool ChangePassword(string userName, string currentPassword, string newPassword)
{
var success = false;
try
{
var currentUser = System.Web.Security.Membership.GetUser(userName, true);
success = currentUser.ChangePassword(currentPassword, newPassword);
}
catch (ArgumentException)
{
}
return success;
}
public static bool DeleteUser(string username)
{
return System.Web.Security.Membership.DeleteUser(username);
}
public static int GetUserId(string userName)
{
var user = System.Web.Security.Membership.GetUser(userName);
return (int) user.ProviderUserKey;
}
public static string CreateAccount(string userName, string password)
{
return CreateAccount(userName, password, requireConfirmationToken: false);
}
public static string CreateAccount(string userName, string password, bool requireConfirmationToken = false)
{
var codeFirstMembership = System.Web.Security.Membership.Provider as CodeFirstMembershipProvider;
return codeFirstMembership.CreateAccount(userName, password, requireConfirmationToken);
}
public static string CreateUserAndAccount(string userName, string password)
{
return CreateUserAndAccount(userName, password, propertyValues: null);
}
public static string CreateUserAndAccount(string userName, string password, bool requireConfirmation)
{
return CreateUserAndAccount(userName, password, null, requireConfirmation);
}
public static string CreateUserAndAccount(string userName, string password, IDictionary<string, object> values)
{
return CreateUserAndAccount(userName, password, values, requireConfirmationToken: false);
}
private static string CreateUserAndAccount(string userName, string password, object propertyValues = null,
bool requireConfirmationToken = false)
{
var codeFirstMembership = System.Web.Security.Membership.Provider as CodeFirstMembershipProvider;
//IDictionary<string, object> values = null;
//if (propertyValues != null)
//{
// values = new RouteValueDictionary(propertyValues);
//}
return codeFirstMembership.CreateUserAndAccount(userName, password, requireConfirmationToken);
}
public static List<MembershipUser> FindUsersByEmail(string email, int pageIndex, int pageSize)
{
int totalRecords;
return
System.Web.Security.Membership.FindUsersByEmail(email, pageIndex, pageSize, out totalRecords)
.Cast<MembershipUser>()
.ToList();
}
public static List<MembershipUser> FindUsersByName(string username, int pageIndex, int pageSize)
{
int totalRecords;
return
System.Web.Security.Membership.FindUsersByName(username, pageIndex, pageSize, out totalRecords)
.Cast<MembershipUser>()
.ToList();
}
public static List<MembershipUser> GetAllUsers(int pageIndex, int pageSize)
{
int totalRecords;
return
System.Web.Security.Membership.GetAllUsers(pageIndex, pageSize, out totalRecords).Cast<MembershipUser>().ToList();
}
}
}