Skip to content

Commit

Permalink
ServerApp 부분 일부 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRoh committed Mar 13, 2022
1 parent aa451c2 commit 024d02d
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ExamBlazorAuth.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UseAuth", "UseAuth\UseAuth.csproj", "{F84B034D-5768-473A-A8AE-4A8BDD1BB03A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -40,6 +42,10 @@ Global
{64074E4E-1FC4-41E0-9A1A-52F0100E0987}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64074E4E-1FC4-41E0-9A1A-52F0100E0987}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64074E4E-1FC4-41E0-9A1A-52F0100E0987}.Release|Any CPU.Build.0 = Release|Any CPU
{F84B034D-5768-473A-A8AE-4A8BDD1BB03A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F84B034D-5768-473A-A8AE-4A8BDD1BB03A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F84B034D-5768-473A-A8AE-4A8BDD1BB03A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F84B034D-5768-473A-A8AE-4A8BDD1BB03A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ExamBlazorAuth
# ServerApp
nuget 설치

# WebAssembly
## server
nuget 설치 (5.0.13으로 통일)
Microsoft.AspNetCore.Authentication.JwtBearer
Expand All @@ -11,10 +14,12 @@ LoginController.cs
코드 추가

appsettings.json 환경설정
``` json
"JwtSecurityKey": "RANDOM_KEY_MUST_NOT_BE_SHARED",
"JwtIssuer": "https://localhost",
"JwtAudience": "https://localhost",
"JwtExpiryInDays": 1
```

## shared
LoginModel, LoginResult 추가
Expand All @@ -38,9 +43,11 @@ razor
Login, Logout

_imports.razor
``` csharp
@using WebAssemblyApp.Client.Services
@using WebAssemblyApp.Shared
@using Microsoft.AspNetCore.Components.Authorization
```

App.razor
<AuthorizeRouteView> 추가
Expand Down
25 changes: 25 additions & 0 deletions ServerApp/Data/Models.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.ComponentModel.DataAnnotations;

namespace ServerApp.Data
{
public class Models
{
}
public class LoginModel
{
[Required]
public string Email { get; set; }

[Required]
public string Password { get; set; }

public bool RememberMe { get; set; }
}

public class LoginResult
{
public bool Successful { get; set; }
public string Error { get; set; }
public string Token { get; set; }
}
}
79 changes: 79 additions & 0 deletions ServerApp/Pages/Login.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@page "/login"

<h1>Login</h1>

@if (ShowErrors)
{
<div class="alert alert-danger" role="alert">
<p>@Error</p>
</div>
}

<div class="card">
<div class="card-body">
<h5 class="card-title">Please enter your details</h5>
<div class="form-group">
<label for="email">Email address</label>
<text>@loginModel.Email</text>
</div>
<div class="form-group">
<label for="password">Password</label>
<text>@loginModel.Password</text>
</div>
<button type="submit" class="btn btn-primary" @onclick="HandleLogin" >Submit</button>
</div>
</div>

@code {
public string ReturnUrl { get; set; }

private LoginModel loginModel;
private bool ShowErrors;
private string Error = "";

protected override async Task OnInitializedAsync()
{
loginModel = new LoginModel();
}

private async Task HandleLogin()
{
ShowErrors = false;
//string returnUrl = Url.Content("~/");
//try
//{
// // Clear the existing external cookie
// await HttpContext
// .SignOutAsync(
// CookieAuthenticationDefaults.AuthenticationScheme);
//}
//catch { }
//// *** !!! This is where you would validate the user !!! ***
//// In this example we just log the user in
//// (Always log the user in for this demo)
//var claims = new List<Claim>
//{
// new Claim(ClaimTypes.Name, loginModel.Email),
// new Claim(ClaimTypes.Role, "Administrator"),
//};
//var claimsIdentity = new ClaimsIdentity(
// claims, CookieAuthenticationDefaults.AuthenticationScheme);
//var authProperties = new AuthenticationProperties
//{
// IsPersistent = true,
// RedirectUri = this.Request.Host.Value
//};
//try
//{
// await HttpContext.SignInAsync(
// CookieAuthenticationDefaults.AuthenticationScheme,
// new ClaimsPrincipal(claimsIdentity),
// authProperties);
//}
//catch (Exception ex)
//{
// string error = ex.Message;
//}
//return LocalRedirect(returnUrl);
}
}
22 changes: 22 additions & 0 deletions ServerApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -9,6 +11,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace ServerApp
Expand All @@ -26,9 +29,23 @@ public Startup(IConfiguration configuration)
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(
CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();

services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();

services.AddHttpContextAccessor();
services.AddScoped<HttpContextAccessor>();
services.AddHttpClient();
services.AddScoped<HttpClient>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -50,6 +67,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseRouting();

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();

app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
Expand Down
9 changes: 9 additions & 0 deletions ServerApp/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
@using Microsoft.JSInterop
@using ServerApp
@using ServerApp.Shared

@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication.Cookies
@using Microsoft.AspNetCore.Mvc
@using Microsoft.AspNetCore.Mvc.RazorPages
@using Microsoft.AspNetCore.Http
@using System.Security.Claims
@using System.Security.Policy
@using ServerApp.Data

0 comments on commit 024d02d

Please sign in to comment.