There is no such thing as a better way, and the answer is always it depends , although it is very useless. For example, you can use forms authentication . If the web application using the web service and the web service is part of the same ASP.NET application , the browser can seamlessly send a form authentication ticket (cookie) for each call to the web service.
In the configuration file you can have
<authorization> <deny users="?" /> </authorization>
This will restrict access to anonymous users and will also cover web services. In other words, if the user does not log in and does not receive a valid cookie with a ticket, the service cannot be used. Of course you must HTTPS . Otherwise, any of them may receive a cookie in the headers and call your service.
Finally, it is impossible to ensure that no one calls the web service outside of your application in the absolute sense. The above approach ensures that none of them visit your service. But there is no way to guarantee that a valid user calls the service directly outside of your application, since the caller of the web service is JavaScript, and any security that you create in JavaScript can be easily understood by the user by looking at the script or even looking at traffic from and to the browser. Any end user who is a bit technical can play requests or modify requests and send them to the web service using valid credentials.
EDIT: -
Here is the more detailed information you are looking to enable FormsAuthentication.
(1) In Web.config , under <system.web> , make sure you have this.
<authentication mode="Forms"> <forms loginUrl="Login.aspx" defaultUrl="~/" /> </authentication> <authorization> <deny users="?" /> </authorization>
This ensures that all unauthenticated (anonymous) users are redirected to Login.aspx.
(2) Enter Login.aspx login logic to get the user ID and password and check them against the database or something else. After successful authentication, that is, the user enters the identifier and password that match what you have in the database, maybe in the handler of the click of the login button, set the ticket.
protected void Button1_Click(object sender, EventArgs e) { // Do all your login logic here // once user ID and password entered by the user are okay, call this string ticket = FormsAuthentication.Encrypt( new FormsAuthenticationTicket("userId", false, 15)); HttpCookie FormsCookie = new HttpCookie( FormsAuthentication.FormsCookieName, ticket) { HttpOnly = true }; HttpContext.Current.Response.Cookies.Add(FormsCookie); }
(3) Add PrincipalPermissionAttribute to a web method like this.
public class Users : System.Web.Services.WebService { [PrincipalPermissionAttribute(SecurityAction.Demand)] [WebMethod] public List<User> GetUsers() {
If you go to any page, you will be redirected to login.aspx, where you will need to enter a user ID and password and login. When you enter the file, a forms authentication cookie is created and recorded. From now on, all requests to your application will be loaded into a cookie (the browser will do this for you). Without logging in, if you go to the web service directly, you will still be redirected to the login page. As I mentioned in my initial answer, a registered user, however, will have access to the web service directly. If JavaScript can do something, the user can do the same.
BTW, web services (asmx) - Deprecated technology.