.net代码实现一个用户注册和登录后用户管理的综合实例
时间: 2024-02-03 18:14:01 浏览: 106
一个演示登录和用户管理的例子
以下是一个简单的 .NET 用户注册、登录和管理的综合示例代码。
1. 创建一个名为 "User" 的类,该类具有以下属性:
```csharp
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public bool IsActive { get; set; }
}
```
2. 创建一个名为 "UserManager" 的类,该类将处理用户的创建、读取、更新和删除操作:
```csharp
public class UserManager
{
private readonly string _connectionString;
public UserManager(string connectionString)
{
_connectionString = connectionString;
}
public User CreateUser(User user)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Users (Username, Password, Email, FullName, IsActive) VALUES (@Username, @Password, @Email, @FullName, @IsActive); SELECT SCOPE_IDENTITY()", connection);
command.Parameters.AddWithValue("Username", user.Username);
command.Parameters.AddWithValue("Password", user.Password);
command.Parameters.AddWithValue("Email", user.Email);
command.Parameters.AddWithValue("FullName", user.FullName);
command.Parameters.AddWithValue("IsActive", user.IsActive);
connection.Open();
user.Id = Convert.ToInt32(command.ExecuteScalar());
}
return user;
}
public User GetUserById(int userId)
{
var user = new User();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Users WHERE Id = @Id", connection);
command.Parameters.AddWithValue("Id", userId);
connection.Open();
var reader = command.ExecuteReader();
if (reader.Read())
{
user.Id = (int)reader["Id"];
user.Username = (string)reader["Username"];
user.Password = (string)reader["Password"];
user.Email = (string)reader["Email"];
user.FullName = (string)reader["FullName"];
user.IsActive = (bool)reader["IsActive"];
}
}
return user;
}
public void UpdateUser(User user)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Users SET Username = @Username, Password = @Password, Email = @Email, FullName = @FullName, IsActive = @IsActive WHERE Id = @Id", connection);
command.Parameters.AddWithValue("Username", user.Username);
command.Parameters.AddWithValue("Password", user.Password);
command.Parameters.AddWithValue("Email", user.Email);
command.Parameters.AddWithValue("FullName", user.FullName);
command.Parameters.AddWithValue("IsActive", user.IsActive);
command.Parameters.AddWithValue("Id", user.Id);
connection.Open();
command.ExecuteNonQuery();
}
}
public void DeleteUser(int userId)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Users WHERE Id = @Id", connection);
command.Parameters.AddWithValue("Id", userId);
connection.Open();
command.ExecuteNonQuery();
}
}
}
```
3. 创建一个名为 "AuthenticationManager" 的类,该类将处理用户身份验证相关的操作:
```csharp
public class AuthenticationManager
{
private readonly UserManager _userManager;
public AuthenticationManager(UserManager userManager)
{
_userManager = userManager;
}
public bool ValidateUser(string username, string password)
{
var user = _userManager.GetUserByUsername(username);
if (user == null)
{
return false;
}
return string.Equals(user.Password, password);
}
}
```
4. 在 Web 应用程序中,使用 ASP.NET Identity 实现用户注册和登录:
```csharp
// 注册新用户
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
// 登录
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
```
5. 在 Web 应用程序中,使用 ASP.NET Identity 实现用户管理:
```csharp
// 获取当前用户
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
// 更新用户
user.Email = model.Email;
user.FullName = model.FullName;
user.IsActive = model.IsActive;
var result = await UserManager.UpdateAsync(user);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
AddErrors(result);
// 删除用户
var result = await UserManager.DeleteAsync(user);
if (result.Succeeded)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
```
这是一个简单的示例,您可以根据自己的需求进行更改和扩展。
阅读全文