Using System.Web.Security in ASP.NET Core 6 - c#

I'm trying to encrypt and decrypt the cookie value but I couldn't.
This is my code
var cookieText = Encoding.UTF8.GetBytes("Text for Cookie");
var encryptedValue = Convert.ToBase64String(MachineKey.Protect(cookieText, "ProtectCookie"));
For this, I have to reference:
using System.Security;
using System.Text;
but the using System.Security; does not work in ASP.NET Core 6.
Do you have any suggestions?

Use Data Protection Provider. It supports your scenario and is available in .NET.

Related

How can I connect to SQL Server and view the data in the database through ASP.NET Web Form?

I am trying to visualize the data which is in the database of SQL Server. For first how am I able to connect my ASP.NET web form to the SQL Server.
I have done: New> project > ASP.NET web application > Web Form
this is a template which has its own register and login form. In the login how can I use the SQL connection code so that it is able to connect and later I can view the table data.
In my code:
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// something like this ?
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyfFrstDataBaseConnectionString"].ConnectionString);
}
}
Please help!!
The easiest way is to use something like Entity Framework. This article explains how to use EF with a webforms application.
This will allow you to work with your data in a strongly typed fashion so you won't need to craft a bunch of SQL queries on your own.
you can use sqlhelper.cs file.
Web.config file set connectionstring
Then enter namespace
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Also Add sqlhelper.cs file namespace in asp.net form.

How to authorize using windows authentication in ASP.net 5

I am creating an application which is going to use windows authentication to allow access to page based on user and its roles. I have gone ahead and configured IIS to use windows authentication and disabled anonymous access. I am able to browse the application without any issue and I have successfully enabled windows authentication for the application.
Now I am trying to set access to controller level by using Authorize, but I am unable to figure it out how it works with ASP.net 5. I am completely new to ASP / C# programming platform but I did search online resources and I came across this and I used below example to give it a try but I get red squiggly line under users and I think ASP.NET 5 is unable to find users. I also made sure that I have added all the references at the top.
[Authorize(Users = #"CONTOSO\Rick, CONTOSO\Keith, CONTOSO\Mike")]
References:
using Microsoft.AspNet.Mvc;
using RMDAAutomation.Models.Repository;
using RMDAAutomation.Models.Entity;
using Microsoft.AspNet.Authorization;
Any suggestions is highly appreciated. :)
UPDATE 1:
TutorialsController.cs:
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Authorization;
namespace RMDAAutomation.Controllers.Web
{
public class TutorialsController : Controller
{
[Authorize(Roles = #"DOMAIN\GROUP")]
public IActionResult Index()
{
return View();
}
}
}
Tutorial Index View:
<div class="container">
<h1>Tutorials</h1>
</div>
Visual studio project settings:
using System.Web.Mvc;
This using solves the issue in my project

Connecting Firebird to an ASP.net WebAPI Project

I'm in the process of learning ASP.net, specifically WebAPI and MVC. I'm using Visual Studio Community 2013, .NET 4.5, and C#. I'm a total newb so I'm actually going through this particular walkthrough to understand how things work:
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
It's been ok so far, but I want to move on to connecting a database to populate my dataset. I'm very familiar with using Firebird and was able to install Firebird as a dataprovider (through NuGet and installing the appropriate DDEX files). Unfortunately, I'm having difficulty understanding how to query the database and populate my array.
Basically, this is what my code looks like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using FirebirdSql.Data.FirebirdClient;
using System.Configuration;
using System.Collections;
namespace NBAPoolWebService.Controllers
{
public class UserController : ApiController
{
User[] users = new User[]
{
new User { ID = 1, CREATED=new DateTime(2011, 1, 12), ISACTIVE='Y', USERNAME="TEST1"},
new User { ID = 2, CREATED=new DateTime(2012, 2, 22), ISACTIVE='Y', USERNAME="TEST2"}
};
public IEnumerable<User> GetAllUsers()
{
return users;
}
public IHttpActionResult GetUser(int id)
{
var user = users.FirstOrDefault((p) => p.ID == id);
if (user == null)
{
return NotFound();
}
return Ok(user);
}
}
}
Currently, if I visit http://localhost/api/user, I get my list of test users via JSON (which is what I want).
What I'm hoping to understand is how to change the array that I set to come from my Firebird database (via an SQL query). I know how to get my query (select * from users), I have a valid connection (confirmed through visual studio) and I have seen some tutorials on how to use FBConnection.
What I'm having difficulty with is actually using the data from the query and populate the objects in the array.
Any help would be greatly appreciated. If you could tell me the piece of code that I need to enter, or point me to the right tutorials, or if you think I should be using a specific technology or methodology, that would be great.
What I'm trying really trying to accomplish/understand is how to create a Web service that responds with JSON and that is connected to a Firebird database.
Thanks in advance for any help!
I've learned a ton about .Net and Entity Framework by watching pluralsight videos. If you don't have an account with them, signing up for the trial would allow you to watch Entity Framework and Data Models. There is a module dedicated to Creating Database First Entity Data Models, which you may find useful.
After a bunch of research, I stumbled upon the following link:
[link]http://johntomaselli.blogspot.ca/2012/04/mvc-4-firebird-ef-41-and-database-first.html
So I deleted my existing User Controller and Model and created a new Model using ADO.NET Entity Data Model (named User).
From there, I used EF Designed from Database and used the connectionstring I created in my web.config.
From there it let me choose the database tables I wanted and built out a Model called User.edmx.
After that, I created a controller using Web API2 Controller with action, using Entity Framework.
This allowed me to choose a model class with the corresponding name.
At this point, I think I need to do some research on the Entity Framework but this at least allows me to create a web service that connects to the database.
Hopefully this helps other young newbs on their start with ASP.net, MVC, WebAPI, Entity Frameworks, and Firebird.
My next steps will be understand the User.edmx and if I should have created a model for all tables (instead of just one). Hopefully I understand that when I resarch the Entity Framework.

ServiceStack "Declaration referenced in a method implementation cannot be a final method"

Trying to set up ServiceStack with OrmLite to connect to my local SQL instance. Getting error
"Declaration referenced in a method implementation cannot be a final
method"
and it's driving me nuts. Here are my steps so far:
New "ServiceStack ASP.NET Empty" project
In the "Service" project, installed ServiceStack.OrmLite.SqlServer
NuGet package
Added the following code to the AppHost "Configure" section:
public override void Configure(Funq.Container container)
{
var connectionString = ConfigurationManager.ConnectionStrings["ApiDbConnectionString"].ConnectionString;
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
OrmLiteConnectionFactory dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);
}
Here are my using statements:
using System;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.SqlServer;
using LRIService.ServiceInterface;
I would like to use this database connection throughout the app for caching, data access, and eventually user authentication.
Lastly, are there any good examples of ServiceStack running as a standalone service (not as part of an ASP.NET MVC app)? I am going to be accessing this via a stand-alone AngularJS app that may or may not be hosted on the same domain (I've worked with the CORS feature before). I'm just having trouble separating the ASP.NET MVC stuff from the ServiceStack stuff in many of the examples on the web.
Thanks for any help...
Your error message sounds like you're mixing different versions of ServiceStack together. Try clearing your NuGet packages Cache. Alternatively you can try the most recent v4.0.34 packages on MyGet.
As for non ASP.NET MVC Examples, most of the ServiceStack Live Demos are Single Page Apps that don't use ASP.NET MVC at all:
Chat (jQuery + ss-utils.js)
Entire App ILMerged into a single cross-platform Chat.exe (Self Host)
React Chat (port to React.js)
Http Benchmarks (jQuery + ss-utils.js)
Email Contacts (jQuery + ss-utils.js)
Stack API's (Angular JS)
Imgur (jQuery)
Todos (Backbone.js)
Razor Rockstars
ASP.NET host
A Stand-alone, self-hosted HttpListener (Self Host)
A Stand-alone Windows Service (Self Host)
Self-hosted HttpListener with
Entire App ILMerged into a single cross-platform App.exe
Hosted inside WinForms with Chromium Embedded Framework
Hosted inside Mac OSX Cocoa App with Xmarain.Mac
REST Files (jQuery)
Redis StackOverflow (jQuery)

WebServices reference issue

I am trying to implement webservices in my porject but the reference can't be found. What might be the issue here? What am I missing?
This is how I reference the Web service. I went to the project root in the solution explorer. Right click and Add Web Reference. It showed the method and everything but when I go in the code to make reference I can't find it.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using net.webservicex.www;
I found the problem. You need to create a new website in order to show all the elements from the webservice. Creating a new Project was not giving me the expected results to be able to connect to the webservice.
I'm a little confused by the comments under the question...
If you are using a web-reference (2.0), you are asked for a "web reference name"; this doubles as your namespace to the item. You can access the service via something like MyWebReferenceName.MyServiceName.
When you use the "add service reference" dialog (3.0+), you are prompted for a "namespace", which acts identically. In this case, the service is available at something like MyWebServiceReferenceName.MyServiceNameClient.

Categories

Resources