concept user in category asp.net

appears as: users, The user, user, A user, user, users
ASP.NET Core in Action, Second Edition

This is an excerpt from Manning's book ASP.NET Core in Action, Second Edition.

  • Letting users log in to your web app
  • Letting users use their Facebook account to log in to your web app using OAuth
  • Figure 3.16 A custom error page created by ExceptionHandlerMiddleware
    . The custom error page can keep the same look and feel as the rest of the application by reusing elements such as the header and footer. More importantly, you can easily control the error details displayed to users.

    In ASP.NET Core web applications, your middleware pipeline will normally include the EndpointMiddleware. This is typically where you write the bulk of your application logic, by calling various other classes in your app. It also serves as the main entry point for users to interact with your app. It typically takes one of three forms:

  • An HTML web application, designed for direct use by users. If the application is consumed directly by users, as in a traditional web application, then Razor Pages is responsible for generating the web pages that the user interacts with. It handles requests for URLs, it receives data posted using forms, and it generates the HTML that users use to view and navigate your app.
  • You’ll also commonly use the various redirect-based results to send the user to a new web page. For example, when you place an order on an e-commerce website you typically navigate through multiple pages, as shown in figure 4.12. The web application sends HTTP redirects whenever it needs you to move to a different page, such as when a user submits a form. Your browser automatically follows the redirect requests, creating a seamless flow through the checkout process.

    Figure 4.12 A typical POST, REDIRECT, GET flow through a website. A user sends their shopping basket to a checkout page, which validates its contents and redirects to a payment page without the user having to manually change the URL.
    ASP.NET Core in Action

    This is an excerpt from Manning's book ASP.NET Core in Action.

    1.5.2. If you’re a Linux or macOS user

    As a Linux or macOS user, you have a whole host of choices. OmniSharp has plugins for most popular editors, such as Vim, Emacs, Sublime, Atom, and Brackets, not to mention the cross-platform Visual Studio Code. Install the appropriate plugin to your favorite and you’ll be writing C# in no time.

    Creating an app is great for seeing an example of how the pieces fit together, but you’ll often need to add users to an existing app. In section 14.4, you’ll see how you can extract the Identity elements from the default template and apply it to an existing app: the recipe application from chapters 12 and 13.

    In section 14.5, you’ll customize the login process. It’s common to need to store additional information about a user (such as their name or date of birth) and to provide permissions to them that you can later use to customize the app’s behavior (if the user is a VIP, for example).

    Figure 14.1. The principal is the current user, implemented as ClaimsPrincipal. It contains a collection of Claims that describe the user.

    14.5. Managing users: adding new claims to users

    Very often, the next step after adding Identity to an application is to customize it. The default templates only require an email and password to register. What if you need more details, like a friendly name for the user? Also, I’ve mentioned that we use claims for security, so what if you want to add a claim called IsAdmin to certain users?

    Listing 14.10. Adding a custom claim to a new user in the Register action
    public async Task<IActionResult> Register( RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { #1 UserName = model.Email, Email = model.Email }; #1 var result = await _userManager.CreateAsync( #2 user, model.Password); #2 if (result.Succeeded) { var claim = new Claim("FullName", model.Name); #3 await _userManager.AddClaimAsync(user, nameClaim); #4 await _signInManager.SignInAsync(user); #5 return RedirectToLocal(returnUrl); } AddErrors(result); } return View(model); } #1 Creates an instance of the ApplicationUser entity, as usual #2 Validates that the provided password is valid and creates the user in the database #3 Creates a claim, with a string name of “FullName” and the provided value #4 Adds the new claim to the ApplicationUser’s collection #5 Signs the user in by setting the HttpContext.User, the principal will include the custom claim !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":6,\"ch\":67},{\"line\":6,\"ch\":69}],[{\"line\":7,\"ch\":67},{\"line\":7,\"ch\":69}],[{\"line\":25,\"ch\":0},{\"line\":25,\"ch\":2}],[{\"line\":6,\"ch\":67},{\"line\":6,\"ch\":69}],[{\"line\":7,\"ch\":67},{\"line\":7,\"ch\":69}],[{\"line\":25,\"ch\":0},{\"line\":25,\"ch\":2}],[{\"line\":8,\"ch\":67},{\"line\":8,\"ch\":69}],[{\"line\":9,\"ch\":67},{\"line\":9,\"ch\":69}],[{\"line\":26,\"ch\":0},{\"line\":26,\"ch\":2}],[{\"line\":8,\"ch\":67},{\"line\":8,\"ch\":69}],[{\"line\":9,\"ch\":67},{\"line\":9,\"ch\":69}],[{\"line\":26,\"ch\":0},{\"line\":26,\"ch\":2}],[{\"line\":13,\"ch\":12},{\"line\":13,\"ch\":58}],[{\"line\":13,\"ch\":67},{\"line\":13,\"ch\":69}],[{\"line\":27,\"ch\":0},{\"line\":27,\"ch\":2}],[{\"line\":14,\"ch\":12},{\"line\":14,\"ch\":62}],[{\"line\":14,\"ch\":67},{\"line\":14,\"ch\":69}],[{\"line\":28,\"ch\":0},{\"line\":28,\"ch\":2}],[{\"line\":16,\"ch\":67},{\"line\":16,\"ch\":69}],[{\"line\":29,\"ch\":0},{\"line\":29,\"ch\":2}]]"} !@%STYLE%@!
    ASP.NET 4.0 in Practice

    This is an excerpt from Manning's book ASP.NET 4.0 in Practice.

    As a user, you want to interact with the page in the easiest way possible. If you need to correct a value, it’s easier to find it than to do some rewriting.

    When a user accesses the default. aspx page as anonymous, the UrlAuthorizationModule intercepts the request and automatically redirects the user to the login page, passing the return URL via the ReturnUrl query-string parameter. After the login, the user is redirected to the requested page; if the parameter is empty, the defaultUrl attribute from web.config is used. You can see this mechanism in action in figure 11.3.

    Figure 11.3. The login page displays username as the current user after the user login. You can customize this page so the user can find the most relevant stuff when they’re logged in.
    Table 11.4. ASP.NET security controls

    Name

    Description

    ChangePassword Changes the password of the user who is currently logged in.
    CreateUserWizard Creates a new user.
    Login Provides the login form and authenticates the user.
    LoginName Displays the current username.
    LoginStatus Shows a login link when the user is anonymous, or a logout link when the user is logged in.
    LoginView Provides an alternate view for anonymous and logged-in users.
    PasswordRecovery Recovers a user password.

    The Membership API lets you quickly implement all you need to use security in your application, but sometimes you just need to group users to simplify policy management. If this is what you need to do, the Roles API was invented for you.

    Table 11.5. Roles API most-used methods

    Name

    Description

    AddUsersToRoles Adds the specified usernames to the passed roles.
    CreateRole Creates a new role.
    DeleteRole Deletes an existing role.
    FindUsersInRole Finds all users in a given role.
    GetAllRoles Retrieves the complete list of roles.
    GetRolesForUser Gets the roles for the specified username.
    IsUserInRole Determines whether the user is in the specified role.
    RemoveUsersFromRoles Removes the users from the passed roles.
    RoleExists Checks whether a role already exists.
    ASP.NET MVC 2 in Action

    This is an excerpt from Manning's book ASP.NET MVC 2 in Action.

    The next step is to create a model. The model doesn’t have to be any particular type of object or inherit from any special class. It can be any class at all. Let’s create a model class that represents the data that the user will be posting back to the server (listing 1.9).

    Ajax (short for Asynchronous JavaScript and XML) is a term coined by Jesse James Garrett to describe a clever technique to make web applications more dynamic, and it has introduced a new era of web applications. It’s a technique that uses the browser’s JavaScript capability to send a request to the server asynchronously. This enables applications to become richer and more user-friendly by updating small sections of the page without requiring a brutal full-page refresh. In today’s web, the vast majority of major websites use this technique to their advantage. Users are demanding this type of rich, seamless interaction with websites. You aren’t going to let them down, are you?

    The first task is to hook into the click event of each link. When the user clicks a list item, as coded in listing 12.10, an Ajax call will be made to get the speaker details (as JSON) and construct a small detail box alongside the link.

    Next we have to do something when the user clicks the link. We added a hidden <div> tag on the page that serves as the container for the speaker’s detailed information. The show_details() function, in listing 12.11, should show this box along with an Ajax loading indicator. When the data comes back from the server, we’ll build elements to display the information.

    Listing 12.11. When the user clicks on the link

    This function has a lot going on, so let’s break it down for each step. The link itself is passed into the function, and we need to retrieve the box element to put the speaker details in, so we use the jQuery $() along with a CSS selector to retrieve it . We then show a spinning indicator to let the user know that something is happening.

    ASP.NET MVC in Action with MvcContrib, NHibernate, and More

    This is an excerpt from Manning's book ASP.NET MVC in Action with MvcContrib, NHibernate, and More.

    In the body of this view, the server-side tags are pulling objects out of ViewData and rendering them in line with HTML. The responsibility of the view is to take objects in ViewData and render them for consumption by the user. The view does not decide what to render, only how to render. The controller has already decided what needs to be rendered.

    AJAX, or asynchronous JavaScript and XML, is a term coined by Jesse James Garret describing a clever new technique to make web applications more dynamic. AJAX introduced a new era of web applications. It’s a technique that uses the browser’s JavaScript capability to send a request to the server asynchronously. This enables applications to become richer and more user-friendly by updating small sections of the page without requiring a brutal full-page refresh. In today’s web, the vast majority of major websites have leveraged this technique to their advantage. Users are demanding this type of rich, seamless interaction with websites. You aren’t going to let them down, are you?

    The first task is to hook into the mouseover and mouseout events of each list item. When the user hovers over a list item, as coded in listing 9.10, an AJAX call will be made to get the speaker details (as JSON) and construct a small detail box alongside the link.

    Next we have to actually do something when the user hovers over the link. I added a hidden <div> tag to each list item that serves as the container for the speaker’s detailed information. The show_details() function, in listing 9.11, should show this box along with an AJAX loading indicator. When the data comes back from the server, we’ll build elements to display the information.

    Listing 9.11. When the user is hovering over the link

    In this function we are simply creating a few HTML elements to display the user details, and we are adding them to the box element. The last thing to do is hide the box, as coded in listing 9.13, when the user leaves the link region.

    Listing 9.13. When the user leaves the link
    function hide_details(link) { $(link).next().hide(); }
    ASP.NET AJAX in Action

    This is an excerpt from Manning's book ASP.NET AJAX in Action.

    Before the introduction of Ajax, a page being refreshed was an indication to most users that something was being processed or that their actions were accepted. Now, with the suppression of the normal postback, users have no indication that something is happening in the background until it’s complete. They need some sort of visual feedback notifying them that work is in progress.

    The UpdateProgress control offers a solution to this problem. Its purpose is to provide a visual cue to the user when an asynchronous postback is occurring. To please the HR director, you add the following snippet of code to the end of the page:

    <asp:UpdateProgress ID="UpdateProgress1" runat="server">
      <ProgressTemplate>
        <img src="images/indicator.gif" />&nbsp;&nbsp;Loading ...
      </ProgressTemplate>
    </asp:UpdateProgress>

    When you run the application again, the visual cue appears when the user selects a new department (see figure 1.13).

    Figure 1.13. It’s generally a good practice to inform users that work is in progress during an asynchronous update.
    Listing 4.14. Raising the AsyncPostBackError event before the dialog is displayed to the user
    protected void Page_Load(object sender, EventArgs e) { ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page); scriptManager.AsyncPostBackError += new EventHandler<AsyncPostBackErrorEventArgs>(OnAsyncPostBackError); } void OnAsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e) { ScriptManager.GetCurrent(this.Page).AsyncPostBackErrorMessage = "We're sorry, an unexpected error has occurred."; }

    If the asynchronous postback hasn’t been canceled or aborted in the previous event, the next step in the timeline is the beginRequest event. Raised just before the asynchronous postback is sent to the server, this occasion is typically used to relay to the user a visual cue that an asynchronous process is about to begin. When a process can end up being lengthy, it’s important to keep the user in tune with the application by providing instant feedback.

    7.2.5. Notifying the user

    Just before an asynchronous request is sent to the server, the PageRequestManager raises the beginRequest event. Similar to the previous example, the BeginRequestEventArgs passed into this handler includes the postBackElement property. When raised, this occurrence gives you the opportunity to notify the user about the upcoming postback before it begins. For lengthy operations, what typically happens (and is recommended) is that the user is given a visual prompt signifying that work is in progress. The prompt is removed when the process is completed. Listing 7.11 demonstrates how you add this behavior to the existing application.

    Listing 7.11. Show and hide a visual prompt to the user during asychronous operations.

    During the postback, the visual prompt you’d like to display to the user is declared in a div element called loadingPanel. When the onBeginRequest function is invoked, the element is displayed by changing its style. To complete the process, when the onEndRequest function is called, you hide the element by setting the style back to its original state. The next step is the server-side processing of the request.

    ASP.Net 2.0 Web Parts in Action: Building Dynamic Web Portals

    This is an excerpt from Manning's book ASP.Net 2.0 Web Parts in Action: Building Dynamic Web Portals.

    The portal example we are about to create will consist of a single web page that has two zones: a zone on the left side of the page and another on the right side. Each zone will contain a single web part. The page will also have a button which enables the user to switch the page into a mode that allows the web parts to be moved between the two zones. When web parts are in this mode, properties, such as their titles, can be edited at runtime. Figure 1.2 shows what our example page will look like when it is complete, and highlights some of the key points.

    Figure 1.2. This example portal has two zones (numbers 1 and 2) between which you can drag web parts. There is also an editor that allows users to customize the properties of the web parts.

    These days, users expect much more from websites and generally require them to be more customizable than their predecessors. Instead of being presented with a simple list of media soundtracks, users can now often personalize a page so that it contains tracks from their favorite artist or music genre. Quite often users can even aggregate their favorite items on a single page through sites that offer more advanced customization services. Sites offering the ability to personalize content become “stickier” as a result, because users stay on the sticky site rather than browsing to other sites for the remainder of the content they want. The benefit for the owners of sticky web-sites is that their sites serve more ads and thus generate greater revenue.

    The code that allows us to toggle the personalization scope between per-user scope and shared scope is similarly trivial, because that functionality is also exposed via helper methods which we access from the WebPartManager. The ToggleScope functionality alternates the personalization scope for the page between shared scope and per-user scope. We use this feature to switch the page into shared scope mode to see how changes made while in that mode affect all users of the portal, and not just the current user.

    protected void lnkToggle_Click(object sender, EventArgs e) {
            WebPartManager1.Personalization.ToggleScope();
    }

    The Reset Personalization button calls the ResetPersonalizationState method of the WebPartManager’s personalization property, and causes any personalization changes made to a page by a user for a given scope to be reset.

    Figure 6.7. The Adventure Works portal provides administrative users with the ability to easily toggle the personalization scope between shared scope and per-user scope.

    As we see from the image, there will be a message displayed just below the page header which shows the user what the current scope of the page is and provides a link allowing him to toggle the scope.