question

mohsenMJ-8783 avatar image
0 Votes"
mohsenMJ-8783 asked BruceZhang-MSFT commented

How to set a username and password for browses a URL?

Hello,
How can I set a username and password for a specific URL like "https://example.com/login"? When someone browses that address then he\she must provide a username and password to visit that address.


Thank you.

windows-server-iis
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BruceZhang-MSFT avatar image
0 Votes"
BruceZhang-MSFT answered

Hi @mohsenMJ-8783 ,

You have two ways to achieve this funtion.

First, you can set it in application via code. Assuming that you use asp.net application, you can use identity to manage users, passwords, profile data, roles, claims, tokens, email confirmation, and more. It will ask users to enter their password and username in any URL you want to set.


   [HttpPost]
     [AllowAnonymous]
     [ValidateAntiForgeryToken]
     public async Task<ActionResult> Register(RegisterViewModel model)
     {
         if (ModelState.IsValid)
         {
             var user = new ApplicationUser() { UserName = model.UserName };
             var result = await UserManager.CreateAsync(user, model.Password);
             if (result.Succeeded)
             {
                 await SignInAsync(user, isPersistent: false);
                 return RedirectToAction("Index", "Home");
             }
             else
             {
                 AddErrors(result);
             }
         }
        
         // If we got this far, something failed, redisplay form
         return View(model);
     }

You can combine it with authentication filter to ask for user credentials and authorization filter to manage user premission in any Action. The advantage of using this method is that it stores user data in the database, and you can customize the number of users and the personal information that users need to fill in.

Second, you can achieve it in IIS manager. Deploy the application on IIS and find the login file. Then switch to feature view. Click authentication module and disable anonymous authentication, enable any other authentication. It is easy to set and don't need to write code. The disadvantage of it is that you cannot customiza any other user information when using windows authentication. It asks for account that exist in user group of windows server. You need to add or delete users manually on windows server.
136464-3.jpg



If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


Best regards,
Bruce Zhang



3.jpg (35.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

mohsenMJ-8783 avatar image
0 Votes"
mohsenMJ-8783 answered BruceZhang-MSFT commented

Hello,
How can I find the login file? When I enter "https://example.com/login", then how can I find the location of that page?

Thank you.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @mohsenMJ-8783 ,

It depends on the application you used. For example, I deployed an asp.net mvc application on IIS and choose it. Then click content view and open the Views folder. All pages whose suffix is .cshtml will be displayed. Choose the index.cshtml and right click to switch to feature view. You will see the authentication module.
137262-3.jpg
137263-4.jpg


0 Votes 0 ·
3.jpg (35.9 KiB)
4.jpg (25.9 KiB)

Hello,
Thank you for your help.
When I enter "https://example.com/login" then, "login" is a file. How can I find its location?

0 Votes 0 ·

Hi @mohsenMJ-8783 ,

Sorry I cannot understand what you mean. My comment is to enable authentication for the login file. Isn't it?

I think you can show some images or steps what you have done if I misunderstand your issue.

0 Votes 0 ·
mohsenMJ-8783 avatar image
0 Votes"
mohsenMJ-8783 answered BruceZhang-MSFT commented

Hello,
Thank you so much.
First of all, I must find the login file? I mean is "login.asp" or "login.aspx".

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @mohsenMJ-8783 ,

First choose the site. Then click the content view.
138376-3.jpg

It will show the folders and files. In my asp.net mvc application, it looks like this:
138353-4.jpg

I can find the index.cshtml in views-> home.
138328-5.jpg

You can follow this way to find the login.aspx in your site.

0 Votes 0 ·
3.jpg (86.0 KiB)
4.jpg (33.5 KiB)
5.jpg (24.9 KiB)