R
R
Ruslan2019-04-10 14:15:42
ASP.NET
Ruslan, 2019-04-10 14:15:42

How to authenticate in ASP.NET WebApi using [System.Web.Http.Authorize] attribute?

Hello.
I have a method in a WebApi controller which looks like this:

[Authorize]
public string Get(int x)
{
    return "Hello";
}

The server also has a database with logins and passwords, authentication methods, Membership and Role providers, etc. mechanisms that can authenticate a user by login and password.
And here are the questions:
1. With the help of what code (what should be done on the server?) I can send something to the client (for example, a token) so that his requests to the specified method on the server are authorized by the [Authorize] attribute
2. What should be sent to the server from the client side (Windows Form application) (preferably using RestSharp) so that the request passes authorization?
My assumptions:
Perhaps there is some way, such as FormsAuthentication.SetAuthCookie(UserName), which generates a token that needs to be passed to the client and the client, adding it to its header, will automatically be authorized through the internal mechanisms of ASP.NET. I wonder if this approach can be used?
Thank you for your attention and your tips, answers, help...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question