渚  直樹

渚 直樹

1633917660

ASP.NETMVCのセッションを使用したシンプルなログインアプリケーション

この記事では、次のことについて説明します。

  • 作成する方法ASP.NET MVCプロジェクトを
  • ADO.NETエンティティデータモデルを追加する方法
  • コントローラを追加する方法
  • ユーザー資格情報を検証する方法
  • セッションでユーザーの詳細を保持し、ユーザーのダッシュボードに表示する方法

手順:データベースを設計する

次のスクリプトを使用して、UserProfileテーブルを作成します。
 

Create Table UserProfile  
    (  
        UserId int primary key identity(1, 1),  
        UserName varchar(50),  
        Password varchar(50),  
        IsActive bit  
    )    

次のスクリプトを使用してユーザーレコードを挿入します。
 

Insert into UserProfile  
Select 'jaipal', 'jai1234', 1 Union All  
Select 'praveen', 'praveen1234', 1 Union All  
Select 'pruthvi', 'pruthvi1234', 1 

ステップ1:プロジェクトを作成する

 

行くFILE新規作成]をクリックし、その後、プロジェクト

プロジェクトを作成する


[インストールされたテンプレート]で[VisualC#、Web]を選択します。その後、ASP.NET MVC 4 Webアプリケーションを選択し、必要に応じてアプリケーション名(MvcLoginAppDemo)とソリューション名を指定して、[OK]をクリックします。

ASP.NET MVC 4Webアプリケーション

[プロジェクトテンプレート]で、テンプレートを[基本]として選択し、エンジンを[ Razor]として表示します。[OK]をクリックします。

基本としてテンプレートを選択します

 

ステップ2:エンティティデータモデルを追加する

ソリューションエクスプローラーに移動し、[プロジェクト]、[追加]の順に右クリックして、[ ADO.NETエンティティデータモデル]を選択します。

ADO.NETエンティティデータモデルを選択します
 

意味のあるモデル名を付けて、[追加]をクリックします。


sampleDataModel

[データベースから生成 ]を選択し、[次へ]をクリックします。

データベースから生成

[新しい接続]をクリックし、

新しい接続
 

[新しい接続]をクリックした後、次のウィザードで次の接続プロパティを指定する必要があります。

  • サーバー名を入力します。
  • [SQLServer認証を使用する]ラジオボタンを選択します。
  • パスワードテキストボックスにユーザー名とパスワードを入力します。
  • 「パスワードを保存する」チェックボックスをオンにします。
  • [データベース名を選択または入力する]ラジオボタンを選択します。
  • 接続を設定するデータベースを選択します。
  • [接続のテスト]ボタンをクリックして、接続を確立できることを確認します。
  • 次に、[OK]をクリックします。
     

テスト接続
 

ラジオボタンの選択:はい、接続文字列に機密データを含めます

データ接続を選択してください

 

次の図のように、データベースオブジェクトを選択します。

データベースオブジェクトを選択してください


 

 [完了]をクリックします。この時点で、UserProfieエンティティが作成されます。
 

UserProfie

手順3:コントローラーを追加する

ソリューションエクスプローラーに移動し、[コントローラー]フォルダーを右クリックし、[追加]をクリックして、[コントローラー]をクリックします。


(または)ショートカットキーCtrl + M、Ctrl + C、

コントローラーを追加する

コントローラー名とスキャフォールディングテンプレートを空のMVCコントローラーとして指定します。次に、[追加]をクリックします。

空のMVCコントローラー

HomeControllerで次のコードを記述します。

using System.Linq;  
using System.Web.Mvc;  
  
namespace MvcLoginAppDemo.Controllers  
{  
    public class HomeController: Controller  
    {  
        public ActionResult Login()  
        {  
            return View();  
        }  
  
        [HttpPost]  
        [ValidateAntiForgeryToken]  
        public ActionResult Login(UserProfile objUser)   
        {  
            if (ModelState.IsValid)   
            {  
                using(DB_Entities db = new DB_Entities())  
                {  
                    var obj = db.UserProfiles.Where(a => a.UserName.Equals(objUser.UserName) && a.Password.Equals(objUser.Password)).FirstOrDefault();  
                    if (obj != null)  
                    {  
                        Session["UserID"] = obj.UserId.ToString();  
                        Session["UserName"] = obj.UserName.ToString();  
                        return RedirectToAction("UserDashBoard");  
                    }  
                }  
            }  
            return View(objUser);  
        }  
  
        public ActionResult UserDashBoard()  
        {  
            if (Session["UserID"] != null)  
            {  
                return View();  
            } else  
            {  
                return RedirectToAction("Login");  
            }  
        }  
    }  
}  

ステップ4:ビューを作成する

 

ログインアクションメソッドのビューを作成する

次の図のように、ログインアクションメソッドを右クリックし、[ビューの追加]をクリックします。

ビューを作成する 


強く型付けされたビューを作成する

  • ビュー名はアクションメソッド名である必要があります。
  • ビューエンジンをRazorとして選択します。
  • [強く型付けされたビューのチェックボックスを作成する]を選択します。
  • モデルクラスをUserProfile(MvcLoginAppDemo)として選択します
  • 空としてScaffoldテンプレートを選択します
  • [追加]をクリックします

ビューを追加
 

そして、Login.cshtml(ビュー)に次のコードを記述します。 

@model MvcLoginAppDemo.UserProfile  
  
@{  
ViewBag.Title = "Login";  
}  
  
@using (Html.BeginForm("Login", "Home", FormMethod.Post))  
{  
<fieldset>  
<legend>Mvc Simple Login Application Demo</legend>  
  
@Html.AntiForgeryToken()  
@Html.ValidationSummary(true)  
@if (@ViewBag.Message != null)  
{  
<div style="border: 1px solid red">  
@ViewBag.Message  
</div>  
}  
<table>  
<tr>  
<td>@Html.LabelFor(a => a.UserName)</td>  
<td>@Html.TextBoxFor(a => a.UserName)</td>  
<td>@Html.ValidationMessageFor(a => a.UserName)</td>  
</tr>  
<tr>  
<td>  
@Html.LabelFor(a => a.Password)  
</td>  
<td>  
@Html.PasswordFor(a => a.Password)  
</td>  
<td>  
@Html.ValidationMessageFor(a => a.Password)  
</td>  
</tr>  
<tr>  
<td></td>  
<td>  
<input type="submit" value="Login" />  
</td>  
<td></td>  
</tr>  
</table>  
</fieldset>  
}  

ログインビューと同じUserDashBoardアクションメソッドのビューを作成します。そして、UserDashBoard.cshtml(表示)に次のコードを記述します。
 

@ {  
    ViewBag.Title = "UserDashBoard";  
}  
  
< fieldset >  
    < legend > User DashBoard < /legend>  
  
@if(Session["UserName"] != null) { < text >  
        Welcome @Session["UserName"].ToString() < /text>  
} < /fieldset>  

手順5:スタートアップページとして設定

するSolution Explorer、Project、App_Start、RouteConfig.csの順に移動  し、アクション名をIndexからLogin(スタートアップページとしてLogin.cshtml)に変更します。

スタートアップページとして設定

ステップ6:アプリケーションを実行する
 


アプリケーションを実行する 


ユーザーの資格情報を入力し、[OK]をクリックします。有効なユーザー資格情報を指定すると、ダッシュボードにユーザー名が表示されます。

ユーザーダッシュボード

リンク: https://www.c-sharpcorner.com/article/simple-login-application-using-Asp-Net-mvc/

#aspdotnet 

What is GEEK

Buddha Community

ASP.NETMVCのセッションを使用したシンプルなログインアプリケーション
Einar  Hintz

Einar Hintz

1602560783

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

To demonstrate CRUD operations – insert, update, delete and retrieve, the project will be dealing with details of a normal bank transaction. GitHub repository for this demo project : https://bit.ly/33KTJAu.

Sub-topics discussed :

  • Form design for insert and update operation.
  • Display forms in modal popup dialog.
  • Form post using jQuery Ajax.
  • Implement MVC CRUD operations with jQuery Ajax.
  • Loading spinner in .NET Core MVC.
  • Prevent direct access to MVC action method.

Create ASP.NET Core MVC Project

In Visual Studio 2019, Go to File > New > Project (Ctrl + Shift + N).

From new project window, Select Asp.Net Core Web Application_._

Image showing how to create ASP.NET Core Web API project in Visual Studio.

Once you provide the project name and location. Select Web Application(Model-View-Controller) and uncheck HTTPS Configuration. Above steps will create a brand new ASP.NET Core MVC project.

Showing project template selection for .NET Core MVC.

Setup a Database

Let’s create a database for this application using Entity Framework Core. For that we’ve to install corresponding NuGet Packages. Right click on project from solution explorer, select Manage NuGet Packages_,_ From browse tab, install following 3 packages.

Showing list of NuGet Packages for Entity Framework Core

Now let’s define DB model class file – /Models/TransactionModel.cs.

public class TransactionModel
{
    [Key]
    public int TransactionId { get; set; }

    [Column(TypeName ="nvarchar(12)")]
    [DisplayName("Account Number")]
    [Required(ErrorMessage ="This Field is required.")]
    [MaxLength(12,ErrorMessage ="Maximum 12 characters only")]
    public string AccountNumber { get; set; }

    [Column(TypeName ="nvarchar(100)")]
    [DisplayName("Beneficiary Name")]
    [Required(ErrorMessage = "This Field is required.")]
    public string BeneficiaryName { get; set; }

    [Column(TypeName ="nvarchar(100)")]
    [DisplayName("Bank Name")]
    [Required(ErrorMessage = "This Field is required.")]
    public string BankName { get; set; }

    [Column(TypeName ="nvarchar(11)")]
    [DisplayName("SWIFT Code")]
    [Required(ErrorMessage = "This Field is required.")]
    [MaxLength(11)]
    public string SWIFTCode { get; set; }

    [DisplayName("Amount")]
    [Required(ErrorMessage = "This Field is required.")]
    public int Amount { get; set; }

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime Date { get; set; }
}

C#Copy

Here we’ve defined model properties for the transaction with proper validation. Now let’s define  DbContextclass for EF Core.

#asp.net core article #asp.net core #add loading spinner in asp.net core #asp.net core crud without reloading #asp.net core jquery ajax form #asp.net core modal dialog #asp.net core mvc crud using jquery ajax #asp.net core mvc with jquery and ajax #asp.net core popup window #bootstrap modal popup in asp.net core mvc. bootstrap modal popup in asp.net core #delete and viewall in asp.net core #jquery ajax - insert #jquery ajax form post #modal popup dialog in asp.net core #no direct access action method #update #validation in modal popup

Hire ASP.Net Developers

Looking to outsource your asp dot net development requirement?

ASP.Net is a special feature of the DOT Net framework created by Microsoft. At HourlyDeveloper.io, we have a team of experienced ASP.Net developers who are experts in delivering custom solutions based on your business requirements. Hire ASP.Net Developers who will provide tailored solutions to facilitate your business growth.

Consult with experts: https://bit.ly/3fNpVqr

#hire asp.net developers #asp.net development company #asp.net development services #asp.net development #asp.net developer #asp.net

Einar  Hintz

Einar Hintz

1602564619

MVC User Registration & Login with ASP.NET Core Identity

User registration and authentication are mandatory in any application when you have little concern about privacy. Hence all most all application development starts with an authentication module. In this article, we will discuss the quickest way to use **ASP.NET Core Identity for User Login and Registration **in a new or existing MVC application.

Sub-topics discussed :

  • How to add ASP.NET Core Identity to MVC application.
  • Customize ASP.NET Core Identity.
  • Identity.UI Design Customization.
  • Next step.

Background

ASP.NET Core Identity is an API, which provides both user interface(UI) and functions for user authentication, registration, authorization, etc. Modules/ APIs like this will really be helpful and fasten the development process. It comes with ASP.NET Core Framework and used in many applications before. Which makes the API more dependable and trustworthy.

ASP.NET Core MVC with user authentication can easily be accomplished using Identity.UI. While creating the MVC project, you just need to select Authentication as Individual User Accounts.

Showing how to create an MVC application with ASP.NET Core Identity API

The rest will be handled by ASP.NET Core Identity UI. It already contains razor view pages and backend codes for an authentication system. But that’s not what we want in most of the cases. we want to customize ASP.NET Core Identity as per our requirement. That’s what we do here.

Create an ASP.NET Core MVC Project

First of all, I will create a brand new ASP.NET Core MVC application without any authentication selected. We could add ASP.NET Core Identity later into the project.

In Visual Studio 2019, Go to File > New > Project (Ctrl + Shift + N). From new project window, select ASP.NET Core Web Application.

Create an ASP.NET Core Web application

Once you provide the project name and location. A new window will be opened as follows, Select _Web Application(Model-View-Controller), _uncheck _HTTPS Configuration _and DO NOT select any authentication method. Above steps will create a brand new ASP.NET Core MVC project.

Select Model View Controller templet under .NET Core

#asp.net core article #asp.net core #add asp.net core identity to existing project #asp.net core identity in mvc #asp.net core mvc login and registration #login and logout in asp.net core

ASP.Net Web development Company USA | WebClues Infotech

A versatile Web & Mobile App Development framework technology that is fast, reliable, and easy to use is ASP.NET. ASP.NET gives the developer complete control over development and can be used on any project big or small.

Want to develop a website or mobile app with ASP.NET?

WebClues Infotech with years of experience and a highly skilled development team can be your go-to agency for your Web & Mobile App Development requirements. With an expert developer team of more than 150+ members, WebClues Infotech has successfully delivered more than 1500 projects worldwide.

Want to know more about the ASP.NET framework?

Visit: https://www.webcluesinfotech.com/asp-net-web-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#asp.net web development company #asp.net web development company usa #asp.net development company in india #asp.net development company #.net software development company #hire asp.net developer

Routing in MVC - ASP.NET Core Demystified

ASP.NET Core MVC has introduced quite a few concepts that new (or new-to-ASP.NET) web developers might have some difficulty getting caught up with. My ASP.NET Core Demystified series is designed to help these developers get started building their own custom, full-fledged, working AASP.NET Core applications. In

#asp.net core demystified #mvc - asp.net core #asp.net core #asp.net #programming