mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 18:47:55 +08:00
add login service
This commit is contained in:
8
OpenAuth.App/DTO/LoginRequest.cs
Normal file
8
OpenAuth.App/DTO/LoginRequest.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace OpenAuth.App.DTO
|
||||||
|
{
|
||||||
|
public class LoginRequest
|
||||||
|
{
|
||||||
|
public string UserName { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
|
}
|
15
OpenAuth.App/DTO/LoginResponse.cs
Normal file
15
OpenAuth.App/DTO/LoginResponse.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OpenAuth.App.DTO
|
||||||
|
{
|
||||||
|
public class LoginResponse : Response
|
||||||
|
{
|
||||||
|
public string UserId { get; set; }
|
||||||
|
public string UserName { get; set; }
|
||||||
|
|
||||||
|
public IList<string> UserRoleNames { get; set; }
|
||||||
|
}
|
||||||
|
}
|
9
OpenAuth.App/DTO/Response.cs
Normal file
9
OpenAuth.App/DTO/Response.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace OpenAuth.App.DTO
|
||||||
|
{
|
||||||
|
public abstract class Response
|
||||||
|
{
|
||||||
|
public bool Success { get; set; }
|
||||||
|
|
||||||
|
public string Message { get; set; }
|
||||||
|
}
|
||||||
|
}
|
46
OpenAuth.App/LoginApp.cs
Normal file
46
OpenAuth.App/LoginApp.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using OpenAuth.App.DTO;
|
||||||
|
using OpenAuth.Domain.Interface;
|
||||||
|
using OpenAuth.Domain.Service;
|
||||||
|
|
||||||
|
namespace OpenAuth.App
|
||||||
|
{
|
||||||
|
public class LoginApp
|
||||||
|
{
|
||||||
|
private LoginService _loginService;
|
||||||
|
|
||||||
|
public LoginApp(IUserRepository repository)
|
||||||
|
{
|
||||||
|
_loginService = new LoginService(repository);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LoginResponse Login(LoginRequest request)
|
||||||
|
{
|
||||||
|
var resp = new LoginResponse {UserName = request.UserName};
|
||||||
|
|
||||||
|
var user = _loginService.Login(request.UserName, request.Password);
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
resp.Message = "<22>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
||||||
|
}
|
||||||
|
else if (!user.Password.Equals(request.Password))
|
||||||
|
{
|
||||||
|
resp.Message = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
||||||
|
}
|
||||||
|
else if (!user.Enabled)
|
||||||
|
{
|
||||||
|
resp.Message = "<22><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var role in user.Roles)
|
||||||
|
{
|
||||||
|
resp.UserRoleNames.Add(role.FullName);
|
||||||
|
}
|
||||||
|
resp.Success = true;
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -39,7 +39,11 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="DTO\LoginRequest.cs" />
|
||||||
|
<Compile Include="DTO\LoginResponse.cs" />
|
||||||
|
<Compile Include="LoginApp.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="DTO\Response.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\OpenAuth.Domain\OpenAuth.Domain.csproj">
|
<ProjectReference Include="..\OpenAuth.Domain\OpenAuth.Domain.csproj">
|
||||||
|
9
OpenAuth.Domain/Interface/IUserRepository.cs
Normal file
9
OpenAuth.Domain/Interface/IUserRepository.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
|
namespace OpenAuth.Domain.Interface
|
||||||
|
{
|
||||||
|
public interface IUserRepository
|
||||||
|
{
|
||||||
|
User FindBy(string username);
|
||||||
|
}
|
||||||
|
}
|
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain.Model
|
||||||
{
|
{
|
||||||
public partial class Button
|
public partial class Button
|
||||||
{
|
{
|
@@ -1,4 +1,4 @@
|
|||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain.Model
|
||||||
{
|
{
|
||||||
public partial class DataPermission
|
public partial class DataPermission
|
||||||
{
|
{
|
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain.Model
|
||||||
{
|
{
|
||||||
public partial class Department
|
public partial class Department
|
||||||
{
|
{
|
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain.Model
|
||||||
{
|
{
|
||||||
public partial class Menu
|
public partial class Menu
|
||||||
{
|
{
|
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain.Model
|
||||||
{
|
{
|
||||||
public partial class Role
|
public partial class Role
|
||||||
{
|
{
|
@@ -1,4 +1,4 @@
|
|||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain.Model
|
||||||
{
|
{
|
||||||
public partial class RoleMenuButton
|
public partial class RoleMenuButton
|
||||||
{
|
{
|
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain.Model
|
||||||
{
|
{
|
||||||
public partial class User
|
public partial class User
|
||||||
{
|
{
|
||||||
@@ -16,9 +16,10 @@ namespace OpenAuth.Domain
|
|||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
public string RealName { get; set; }
|
public string RealName { get; set; }
|
||||||
public string RoleId { get; set; }
|
public string RoleId { get; set; }
|
||||||
public Nullable<int> Enabled { get; set; }
|
public bool Enabled { get; set; }
|
||||||
public Nullable<int> DeleteMark { get; set; }
|
public bool DeleteMark { get; set; }
|
||||||
public virtual ICollection<Department> Departments { get; set; }
|
public virtual ICollection<Department> Departments { get; set; }
|
||||||
public virtual ICollection<Role> Roles { get; set; }
|
public virtual ICollection<Role> Roles { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -39,15 +39,18 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Button.cs" />
|
<Compile Include="Interface\IUserRepository.cs" />
|
||||||
<Compile Include="DataPermission.cs" />
|
<Compile Include="Model\Button.cs" />
|
||||||
<Compile Include="Department.cs" />
|
<Compile Include="Model\DataPermission.cs" />
|
||||||
<Compile Include="Menu.cs" />
|
<Compile Include="Model\Department.cs" />
|
||||||
|
<Compile Include="Model\Menu.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Role.cs" />
|
<Compile Include="Model\Role.cs" />
|
||||||
<Compile Include="RoleMenuButton.cs" />
|
<Compile Include="Model\RoleMenuButton.cs" />
|
||||||
<Compile Include="User.cs" />
|
<Compile Include="Model\User.cs" />
|
||||||
|
<Compile Include="Service\LoginService.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup />
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
Other similar extension points exist, see Microsoft.Common.targets.
|
Other similar extension points exist, see Microsoft.Common.targets.
|
||||||
|
26
OpenAuth.Domain/Service/LoginService.cs
Normal file
26
OpenAuth.Domain/Service/LoginService.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using OpenAuth.Domain.Interface;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
|
namespace OpenAuth.Domain.Service
|
||||||
|
{
|
||||||
|
public class LoginService
|
||||||
|
{
|
||||||
|
private IUserRepository _userRepository;
|
||||||
|
|
||||||
|
public LoginService(IUserRepository repository)
|
||||||
|
{
|
||||||
|
_userRepository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User Login(string username, string password)
|
||||||
|
{
|
||||||
|
return _userRepository.FindBy(username);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity.ModelConfiguration;
|
using System.Data.Entity.ModelConfiguration;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure.Mapping
|
namespace OpenAuth.Infrastructure.Mapping
|
||||||
{
|
{
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity.ModelConfiguration;
|
using System.Data.Entity.ModelConfiguration;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure.Mapping
|
namespace OpenAuth.Infrastructure.Mapping
|
||||||
{
|
{
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity.ModelConfiguration;
|
using System.Data.Entity.ModelConfiguration;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure.Mapping
|
namespace OpenAuth.Infrastructure.Mapping
|
||||||
{
|
{
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity.ModelConfiguration;
|
using System.Data.Entity.ModelConfiguration;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure.Mapping
|
namespace OpenAuth.Infrastructure.Mapping
|
||||||
{
|
{
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity.ModelConfiguration;
|
using System.Data.Entity.ModelConfiguration;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure.Mapping
|
namespace OpenAuth.Infrastructure.Mapping
|
||||||
{
|
{
|
||||||
@@ -30,6 +31,16 @@ namespace OpenAuth.Infrastructure.Mapping
|
|||||||
this.Property(t => t.DepartmentId)
|
this.Property(t => t.DepartmentId)
|
||||||
.HasMaxLength(50);
|
.HasMaxLength(50);
|
||||||
|
|
||||||
|
this.HasMany(d => d.Users)
|
||||||
|
.WithMany(u => u.Roles)
|
||||||
|
.Map(
|
||||||
|
m =>
|
||||||
|
{
|
||||||
|
m.MapLeftKey("RoleId");
|
||||||
|
m.MapRightKey("UserId");
|
||||||
|
m.ToTable("UserRole");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Table & Column Mappings
|
// Table & Column Mappings
|
||||||
this.ToTable("Role");
|
this.ToTable("Role");
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity.ModelConfiguration;
|
using System.Data.Entity.ModelConfiguration;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure.Mapping
|
namespace OpenAuth.Infrastructure.Mapping
|
||||||
{
|
{
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity.ModelConfiguration;
|
using System.Data.Entity.ModelConfiguration;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure.Mapping
|
namespace OpenAuth.Infrastructure.Mapping
|
||||||
{
|
{
|
||||||
@@ -36,6 +37,15 @@ namespace OpenAuth.Infrastructure.Mapping
|
|||||||
m.ToTable("UserDepartment");
|
m.ToTable("UserDepartment");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.HasMany(u => u.Roles)
|
||||||
|
.WithMany(r => r.Users)
|
||||||
|
.Map(m =>
|
||||||
|
{
|
||||||
|
m.MapLeftKey("UserId");
|
||||||
|
m.MapRightKey("RoleId");
|
||||||
|
m.ToTable("UserRole");
|
||||||
|
});
|
||||||
|
|
||||||
// Table & Column Mappings
|
// Table & Column Mappings
|
||||||
this.ToTable("User");
|
this.ToTable("User");
|
||||||
this.Property(t => t.UserId).HasColumnName("UserId");
|
this.Property(t => t.UserId).HasColumnName("UserId");
|
||||||
|
@@ -55,6 +55,8 @@
|
|||||||
<Compile Include="Mapping\UserMap.cs" />
|
<Compile Include="Mapping\UserMap.cs" />
|
||||||
<Compile Include="OpenAuthDBContext.cs" />
|
<Compile Include="OpenAuthDBContext.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Repository\BaseRepository.cs" />
|
||||||
|
<Compile Include="Repository\UserRepository.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\OpenAuth.App\OpenAuth.App.csproj">
|
<ProjectReference Include="..\OpenAuth.App\OpenAuth.App.csproj">
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System.Data.Entity;
|
using System.Data.Entity;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
using OpenAuth.Infrastructure.Mapping;
|
using OpenAuth.Infrastructure.Mapping;
|
||||||
|
|
||||||
namespace OpenAuth.Infrastructure
|
namespace OpenAuth.Infrastructure
|
||||||
|
12
OpenAuth.Infrastructure/Repository/BaseRepository.cs
Normal file
12
OpenAuth.Infrastructure/Repository/BaseRepository.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OpenAuth.Infrastructure.Repository
|
||||||
|
{
|
||||||
|
public class BaseRepository
|
||||||
|
{
|
||||||
|
protected OpenAuthDBContext _Context = new OpenAuthDBContext();
|
||||||
|
}
|
||||||
|
}
|
25
OpenAuth.Infrastructure/Repository/UserRepository.cs
Normal file
25
OpenAuth.Infrastructure/Repository/UserRepository.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using OpenAuth.Domain.Interface;
|
||||||
|
using OpenAuth.Domain.Model;
|
||||||
|
|
||||||
|
namespace OpenAuth.Infrastructure.Repository
|
||||||
|
{
|
||||||
|
public class UserRepository :BaseRepository, IUserRepository
|
||||||
|
{
|
||||||
|
public User FindBy(string username)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return _Context.Users.First(e => e.Account == username);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
28
OpenAuth.UnitTest/LoginTest.cs
Normal file
28
OpenAuth.UnitTest/LoginTest.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using OpenAuth.App;
|
||||||
|
using OpenAuth.App.DTO;
|
||||||
|
using OpenAuth.Infrastructure.Repository;
|
||||||
|
|
||||||
|
namespace OpenAuth.UnitTest
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class LoginTest
|
||||||
|
{
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Login()
|
||||||
|
{
|
||||||
|
|
||||||
|
var loginReq = new LoginRequest { UserName = "admin", Password = "123456" };
|
||||||
|
var loginApp = new LoginApp(new UserRepository());
|
||||||
|
var response = loginApp.Login(loginReq);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(response.UserName, loginReq.UserName);
|
||||||
|
|
||||||
|
var errPassword = new LoginRequest { UserName = "admin", Password = "111111" };
|
||||||
|
response = loginApp.Login(errPassword);
|
||||||
|
Assert.IsFalse(response.Success);
|
||||||
|
Assert.AreEqual(response.Message, "密码错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -63,12 +63,17 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="DepartmentTest.cs" />
|
<Compile Include="DepartmentTest.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="LoginTest.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config" />
|
<None Include="App.config" />
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\OpenAuth.App\OpenAuth.App.csproj">
|
||||||
|
<Project>{0BBF2D65-FFFD-4272-B138-8EA4FB6FEC48}</Project>
|
||||||
|
<Name>OpenAuth.App</Name>
|
||||||
|
</ProjectReference>
|
||||||
<ProjectReference Include="..\OpenAuth.Domain\OpenAuth.Domain.csproj">
|
<ProjectReference Include="..\OpenAuth.Domain\OpenAuth.Domain.csproj">
|
||||||
<Project>{6108DA8E-92A1-4ABE-B9F5-26D64D55CA2C}</Project>
|
<Project>{6108DA8E-92A1-4ABE-B9F5-26D64D55CA2C}</Project>
|
||||||
<Name>OpenAuth.Domain</Name>
|
<Name>OpenAuth.Domain</Name>
|
||||||
|
Reference in New Issue
Block a user